如何在ci中的数据库中插入记录

kgsdhlau  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(271)

我的控制器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';
    }

我的model 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);  
     }
}

但问题是我的记录没有插入数据库。。。

q5iwbnjs

q5iwbnjs1#

你做错了。在控制器中看到你对插入模型的调用,你可以从视图中发送控制器中的数据,但不能在模型中发送。所以模型中没有post数据。

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(); 
         }
    }

如果这能解决你的问题,请告诉我。

uqzxnwby

uqzxnwby2#

根据您的代码,代码中没有任何问题。但要确保你的数据库 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,

yr9zkbsy

yr9zkbsy3#

我发现你的代码有问题,首先在hello/index中加载视图页面,然后检查submit按钮的isset;

$submit = $this->input->post('submit');
if(isset($submit)){}

或者,如果要在同一方法上加载视图页,则必须调用窗体操作,如;

<form method="post" action="<?php echo base_url(); ?>hello/index" >

我已经更新了你的密码
控制器.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');
    }
}

模型.php

class insert_model extends CI_Model{
    public function insert_data($data){
        return   $this->db->insert('user',$data); 
    }
}
djmepvbi

djmepvbi4#


**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();  
 }

相关问题