I'm quite new to codeigniter. Currently i'm trying to insert data to my local database from input fields from form. There's no error that popped out but the data that was entered was not showing on my database. I've been reviewing my admin-index file, admin(controller) file and admin_model file. But the code is pretty understandable. Can't find the error. If database connection was the issue, I'm certain that it's successfully connected since I can log in (especially with different usertypes). Below are my codes. Thanks for anyone who could share an idea :)
admin.php(controller)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
//$this->load->model('Admin_model');
}
public function add()
{
$newData = array(
'fname' =>$this->input->post('firstName'),
'lname' =>$this->input->post('lastName'),
'uname' =>$this->input->post('userName'),
'pword' =>$this->input->post('passWord'),
'utype' =>$this->input->post('userType')
);
$this->load->model('Admin_model');
$this->Admin_model->add_user($newData);
}
}
admin_model.php
<?php
class Admin_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function add_user($data)
{
$this->db->insert('tblaccount', $data);
//echo($newData);
}
}
admin-index.php
<form>
<div class="form-group" action="<?php echo base_url(); ?>admin/add" method="POST">
<label>First Name</label>
<input type="text" name="firstName" class="form-control">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" name="lastName" class="form-control">
</div>
<div class="form-group">
<label> Username</label>
<input type="text" name="userName" class="form-control">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="passWord" class="form-control">
</div>
<div class="form-group">
<label>UserType</label>
<input type="text" name="userType" class="form-control">
</div>
<button type="submit" name="submit" value="submit" class="btn btn-primary">Submit</button>
</form>
CodePudding user response:
I think i got it now. i just transfered the action="admin/add" method="POST" line of code from div tag to form tag. It worked!!!. At first I observed the behavior of my POST method in my html form. It's kinda strange cause it's acting like a GET method(user inputs are displayed in url). Then I initially conculed that maybe the method="POST" was not recognized.
Here is my final code on my html form:
<form action="<?php echo base_url(); ?>admin/add" method="POST">
<div class="form-group">
<label>First Name</label>
<input type="text" name="firstName" class="form-control" value="<?php echo set_value('firstName'); ?>">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" name="lastName" class="form-control" value="<?php echo set_value('lastName'); ?>">
</div>
<div class="form-group">
<label> Username</label>
<input type="text" name="userName" class="form-control" value="<?php echo set_value('usernName'); ?>">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="passWord" class="form-control" value="<?php echo set_value('passWord'); ?>">
</div>
<div class="form-group">
<label>UserType</label>
<input type="text" name="userType" class="form-control">
</div>
<button type="submit" name="submit" value="submit" class="btn btn-primary">Submit</button>
</form>
The issue was not on functions (model/controller) at all. btw, Thanks for everyone who shared idea, I really appreciate it :)
CodePudding user response:
print your query inside model function and see what is the error
public function add_user($data)
{
$this->db->insert('tblaccount', $data);
echo $this->db->last_query();
exit();
}