我的控制器Hello.php在这里
class Hello extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('insert_model');
}
public function index(){
$this->insert_model->insert();
$data['message'] = 'Data Inserted Successfully';
}
我的模型insert_model.php这里
class insert_model extends CI_Model{
public function __construct()
{
$this->load->database();
}
public function insert(){
$data=array(
'user_name'=>$this->input->post('user_name'),
'user_email'=>$this->input->post('user_email'),
'user_password'=>$this->input->post('user_password'),
'user_dob'=>$this->input->post('user_dob'),
'user_gender'=>$this->input->post('user_gender'),
'user_address'=>$this->input->post('user_address'),
'user_contact'=>$this->input->post('user_contact')
);
$this->db->insert('user',$data);
}
}
答案 0 :(得分:1)
**in controller**
public function index(){
$data=array(
'user_name'=>$this->input->post('user_name'),
'user_email'=>$this->input->post('user_email'),
'user_password'=>$this->input->post('user_password'),
'user_dob'=>$this->input->post('user_dob'),
'user_gender'=>$this->input->post('user_gender'),
'user_address'=>$this->input->post('user_address'),
'user_contact'=>$this->input->post('user_contact')
);
$last_id = $this->insert_model->insert_data($data);
if($last_id)
$data['message'] = 'Data Inserted Successfully';
**In model**
public function insert(){
$this->db->insert('user',$data);
//check the query by
echo $this->db->last_query();
return $this->db->insert_id();
}
答案 1 :(得分:0)
根据您的代码,代码中没有任何问题。但请确保您的数据库user
表格中包含user_name
,user_email
.... user_contact
'user_name'=>$this->input->post('user_name'),
'user_email'=>$this->input->post('user_email'),
'user_password'=>$this->input->post('user_password'),
'user_dob'=>$this->input->post('user_dob'),
数组键应始终引用表列名称
'column_name'=>value,
答案 2 :(得分:0)
你做错了。看到你在控制器中调用插入模型,你可以从控制器中发送数据,但不能在模型中发送。所以没有后期数据进入模型。
class Hello extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('insert_model');
}
public function index(){
$data=array(
'user_name'=>$this->input->post('user_name'),
'user_email'=>$this->input->post('user_email'),
'user_password'=>$this->input->post('user_password'),
'user_dob'=>$this->input->post('user_dob'),
'user_gender'=>$this->input->post('user_gender'),
'user_address'=>$this->input->post('user_address'),
'user_contact'=>$this->input->post('user_contact')
);
$data['last_id'] = $this->insert_model->insert_data($data);
$data['message'] = 'Data Inserted Successfully';
}
class insert_model extends CI_Model{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function insert_data($data){
$this->db->insert('user',$data);
return $this->db->insert_id();
}
}
如果能解决您的问题,请告诉我。
答案 3 :(得分:0)
我的代码出现了问题,首先在hello / index中加载视图页面 然后检查提交按钮的设置,例如
$submit = $this->input->post('submit');
if(isset($submit)){}
或者,如果您在同一方法上加载视图页面,则必须调用表单操作;
<form method="post" action="<?php echo base_url(); ?>hello/index" >
我已更新您的代码
<强> Controller.php这样强>
class Hello extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('insert_model');
}
public function index(){
$submit = $this->input->post('submit');
if(isset($submit)){
$data=array(
'user_name'=>$this->input->post('user_name'),
'user_email'=>$this->input->post('user_email'),
'user_password'=>$this->input->post('user_password'),
'user_dob'=>$this->input->post('user_dob'),
'user_gender'=>$this->input->post('user_gender'),
'user_address'=>$this->input->post('user_address'),
'user_contact'=>$this->input->post('user_contact')
);
$insert = $this->inser_model-> insert_data();
if($insert) {
$data['message'] = 'Data Inserted Successfully';
}
}
$this->load->view('your_page');
}
}
<强> Model.php 强>
class insert_model extends CI_Model{
public function insert_data($data){
return $this->db->insert('user',$data);
}
}