Iam使用最新版本的CI(3.1.6) 问题是我要插入的数据无法发布到控制器,因此如果我从视图中打印出数据,它就无法从表单中捕获任何数据。我认为这个问题位于表格中。但我不知道如何解决它。谁能帮我? 这是我的代码
我的form.php代码
<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>
我的控制器代码
<?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');
}
}
}?>
我的模式代码
<?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;
}
}
?
答案 0 :(得分:1)
你没有像这样在控制器构造函数上加载数据库类。
function __construct(){
parent::__construct();
$this->load->model('m_register');
$this->load->database();
}
答案 1 :(得分:0)
要解决的第一个问题是模型的方法InsertData()
。您已在模型中定义它,需要两个参数 - $tableName
和$data
public function InsertData($tabelName, $data){
但是在控制器中,您可以在不提供任何参数的情况下调用该方法。
} else {
$this->m_register->InsertData(); //No parameters???
因此,您需要传递参数或更改方法的定义。这是一个更简单的版本,可以完成插入工作,而且您不需要更改控制器。
public function InsertData()
{
$datac = $this->input->post();
return $this->db->insert('member', $datac);
}
您不需要一次获得一个输入。在不传递字段名称的情况下调用input->post()
将返回包含所有字段的数组。这意味着您不必手动创建$datac
数组。
此外,只需返回$this->db->insert
返回的内容而不是将其保存到var然后返回var,就可以减少输入。
现在,为什么没有新插入的数据显示?可能是因为在您拨打$this->m_register->InsertData();
之后,您拨打了$this->load->view('dashboard');
但未提供任何数据用于&#39;信息中心&#39;查看显示。
我认为你真正想做的是redirect
到显示仪表板的控制器/方法。假设控制器为Dashboard
且方法为index()
,do_insert()
的最后一部分应该看起来像这样。
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
}
}
答案 2 :(得分:0)
您无需添加模型。您可以使用这样简单的方法:
<?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";
}
}
}
}?>