Reputation: 157
Iam using the newest version of CI (3.1.6) The problem is the data I want to insert can't post to the controller, so if I print out the data from view, it can't catch any data from the form. I think the problem's located in the form. but I don'r know how to fix it. Can anyone help me? Here's my code
My form.php code
<div id="register" class="text-center">
<div class="container">
<div class="section-title center">
<h2>Register</h2>
<hr>
<form action="" method="post">
<link href="<?php echo base_url();?>assets/user/css/login.css" rel="stylesheet">
<input type="text" name="firstname" placeholder="First Name">
<input type="text" name="lastname" placeholder="Last Name">
<input type="text" name="fullname" placeholder="Full Name">
<input type="text" name="username" placeholder="Username">
<input type="text" name="email" placeholder="Email">
<input type="password" name="pass" placeholder="Password">
<!-- <input type="password" name="pass1" placeholder="Retype Password"> -->
<ul>
<p1>Gender</p1>
<select name="gender" id="" class="pilihan">
<option value="men">Male</option>
<option value="women">Female</option></select>
</ul>
<ul>
<p1>Occupation</p1>
<li><input type="radio" name="job" value="doctor" checked> Doctor<br></li>
<li><input type="radio" name="job" value="nurse"> Nurse<br></li>
</ul>
<link rel="stylesheet" type="text/css" href="assets/user/login.css">
<a href="<?php echo base_url('c_register/do_insert'); ?>" class="btn btn-primary btn-lg active" role="button">Primary link</a>
</form>
my controller code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class C_register extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('m_register');
}
function do_insert(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'firstname', 'required');
$this->form_validation->set_rules('lastname', 'lastname', 'required');
$this->form_validation->set_rules('fullname', 'fullname', 'required');
$this->form_validation->set_rules('username', 'username', 'required');
$this->form_validation->set_rules('email', 'email', 'required');
$this->form_validation->set_rules('pass', 'pass', 'required');
$this->form_validation->set_rules('gender', 'gender', 'required');
$this->form_validation->set_rules('job', 'job', 'required');
if ($this->form_validation->run() === FALSE) {
echo "gagal diinsert";
// $this->load->view('templates/header', $data);
// $this->load->view('news/comment_form');
// $this->load->view('templates/footer');
} else {
$this->m_register->InsertData();
$this->load->view('dashboard');
}
}
}?>
my mode code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_register extends CI_Model {
public function InsertData($tabelName, $data){
$datac = array(
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'fullname' => $this->input->post('fullname'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'pass' => $this->input->post('pass'),
'gender' => $this->input->post('gender'),
'job' => $this->input->post('job')
);
$res = $this->db->insert('member', $datac);
return $res;
}
}
?
Upvotes: 0
Views: 2109
Reputation: 2025
You dont need to add the model. You can use the easy way like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class C_register extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('m_register');
}
function do_insert(){
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'firstname', 'required');
$this->form_validation->set_rules('lastname', 'lastname', 'required');
$this->form_validation->set_rules('fullname', 'fullname', 'required');
$this->form_validation->set_rules('username', 'username', 'required');
$this->form_validation->set_rules('email', 'email', 'required');
$this->form_validation->set_rules('pass', 'pass', 'required');
$this->form_validation->set_rules('gender', 'gender', 'required');
$this->form_validation->set_rules('job', 'job', 'required');
if ($this->form_validation->run() === FALSE) {
echo "gagal diinsert";
} else {
//check in the array that it is same as column name in table like 'firstname' will be present on the table
$datac = array(
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'fullname' => $this->input->post('fullname'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'pass' => $this->input->post('pass'),
'gender' => $this->input->post('gender'),
'job' => $this->input->post('job')
);
$this->db->insert('member', $datac);
$insert_id = $this->db->insert_id();
if(!empty($insert_id)){
$this->load->view('dashboard');
}else{
echo "failed";
}
}
}
}?>
Upvotes: 0
Reputation: 58
You didn,t load the database class on your controller constructor like this.
function __construct(){
parent::__construct();
$this->load->model('m_register');
$this->load->database();
}
Upvotes: 1
Reputation: 8964
The first problem to address is the model's method InsertData()
. You have defined it in the model to require two parameters - $tableName
and $data
public function InsertData($tabelName, $data){
But in the controller you call the method without providing any parameters.
} else {
$this->m_register->InsertData(); //No parameters???
So, you either need to pass the parameters or change the definition of the method. Here is a much simpler version that will get the insert job done and you don't need to change the controller.
public function InsertData()
{
$datac = $this->input->post();
return $this->db->insert('member', $datac);
}
You don't need to get each input one at a time. Calling input->post()
without passing a field name will return an array with all the fields. That means you don't have to manually create the $datac
array.
Also, it's less typing to simply return what $this->db->insert
returns instead of saving it to a var and then returning the var.
Now, why doesn't the newly inserted data display? Probably because after you call $this->m_register->InsertData();
you call $this->load->view('dashboard');
but are not providing any data for the 'dashboard' view to display.
I think what you really want to do is redirect
to the controller/method that shows the dashboard. Assuming the controller is Dashboard
and the method is index()
the last part of do_insert()
should probably look something like this.
if($this->form_validation->run() === FALSE)
{
echo "gagal diinsert";
// $this->load->view('templates/header', $data);
// $this->load->view('news/comment_form');
// $this->load->view('templates/footer');
}
else
{
if($this->m_register->InsertData())
{
redirect('dashboard');
}
else
{
//show an error page or error message about the failed insert
}
}
Upvotes: 0