我目前正在学习本教程https://www.tutorialspoint.com/codeigniter/working_with_database.htm
在这些步骤之前,我不能将记录添加到数据库中,我对Codeigniter很陌生。
螺柱控制器.php

<?php
   class Stud_controller extends CI_Controller {

      function __construct() {
         parent::__construct();
         $this->load->helper('url');
         $this->load->database();
      }

      public function index() {
         $query = $this->db->get("stud");
         $data['records'] = $query->result();

         $this->load->helper('url');
         $this->load->view('Stud_view',$data);
      }

      public function add_student_view() {
        $this->load->helper('form');
        $this->load->view('Stud_add');
      }

      public function add_student() {
         $this->load->model('Stud_Model');

         $data = array(
            'roll_no' => $this->input->post('roll_no'),
            'name' => $this->input->post('name')
         );

         $this->Stud_Model->insert($data);

         $query = $this->db->get("stud");
         $data['records'] = $query->result();
         $this->load->view('Stud_view',$data);
      }

      public function update_student_view() {
         $this->load->helper('form');
         $roll_no = $this->uri->segment('3');
         $query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
         $data['records'] = $query->result();
         $data['old_roll_no'] = $roll_no;
         $this->load->view('Stud_edit',$data);
      }

      public function update_student(){
         $this->load->model('Stud_Model');

         $data = array(
            'roll_no' => $this->input->post('roll_no'),
            'name' => $this->input->post('name')
         );

         $old_roll_no = $this->input->post('old_roll_no');
         $this->Stud_Model->update($data,$old_roll_no);

         $query = $this->db->get("stud");
         $data['records'] = $query->result();
         $this->load->view('Stud_view',$data);
      }

      public function delete_student() {
         $this->load->model('Stud_Model');
         $roll_no = $this->uri->segment('3');
         $this->Stud_Model->delete($roll_no);

         $query = $this->db->get("stud");
         $data['records'] = $query->result();
         $this->load->view('Stud_view',$data);
      }
   }
?>

螺柱模型.php
<?php
   class Stud_Model extends CI_Model {

      function __construct() {
         parent::__construct();
      }

      public function insert($data) {
         if ($this->db->insert("stud", $data)) {
            return true;
         }
      }

      public function delete($roll_no) {
         if ($this->db->delete("stud", "roll_no = ".$roll_no)) {
            return true;
         }
      }

      public function update($data,$old_roll_no) {
         $this->db->set($data);
         $this->db->where("roll_no", $old_roll_no);
         $this->db->update("stud", $data);
      }
   }
?>

Student_add.php(视图)
<!DOCTYPE html>
<html lang = "en">

   <head>
      <meta charset = "utf-8">
      <title>Students Example</title>
   </head>

   <body>
      <form method = "" action = "">

         <?php
            echo form_open('Stud_controller/add_student');
            echo form_label('Roll No.');
            echo form_input(array('id'=>'roll_no','name'=>'roll_no'));
            echo "<br/>";

            echo form_label('Name');
            echo form_input(array('id'=>'name','name'=>'name'));
            echo "<br/>";

            echo form_submit(array('id'=>'submit','value'=>'Add'));
            echo form_close();
         ?>

      </form>
   </body>

</html>

我认为没有路由问题,因为删除功能正在工作。

最佳答案

第一件事:看在上帝的份上,不要加载你创建的每个函数。改用construct。
Check CI Controller pattern too,不需要控制器名称,这样您的路由将非常难看。
我把你也需要注意的地方改了。

<?php
   class Stud extends CI_Controller {

      function __construct() {
         parent::__construct();
         $this->load->helper('url', 'form'); //prefer to put the basics helpers on autoload in config dir, you will use basically in all your project
         $this->load->database(); //you don't need that, just check your confs
         $this->load->model('stud_model'); //check the needs of this model too. If it's too basic, put on autoload
      }

      public function index() {
         $query = $this->db->get("stud");
         $data['records'] = $query->result();

         $this->load->helper('url');
         $this->load->view('Stud_view',$data);
      }

      public function add_student_view() {
        $this->load->view('Stud_add');
      }

      public function add_student() {
         $data = array(
            'roll_no' => $this->input->post('roll_no'),
            'name' => $this->input->post('name')
         );

         $this->stud_model->insert($data); //avoid camelcase and stuff into model object call
         //dont use db access inside controller, this behavior is from model, create a Get function there
         $query = $this->db->get("stud");
         $data['records'] = $query->result();
         $this->load->view('Stud_view',$data);
      }

      public function update_student_view() {
         $roll_no = $this->uri->segment('3');
         //dont use db access inside controller, this behavior is from model, create a Get function there

         $query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
         $data['records'] = $query->result();
         $data['old_roll_no'] = $roll_no;
         $this->load->view('Stud_edit',$data);
      }

      public function update_student(){

         $data = array(
            'roll_no' => $this->input->post('roll_no'),
            'name' => $this->input->post('name')
         );

         $old_roll_no = $this->input->post('old_roll_no');
         $this->stud_Model->update($data,$old_roll_no);
             //dont use db access inside controller, this behavior is from model, create a Get function there

         $query = $this->db->get("stud");
         $data['records'] = $query->result();
         $this->load->view('Stud_view',$data);
      }

      public function delete_student() {
         $roll_no = $this->uri->segment('3');
         $this->stud_model->delete($roll_no);
             //dont use db access inside controller, this behavior is from model, create a Get function there

         $query = $this->db->get("stud");
         $data['records'] = $query->result();
         $this->load->view('Stud_view',$data);
      }
   }
?>

<?php
   class Stud_Model extends CI_Model {
   //create Get and Set functions with private variables
      function __construct() {
         parent::__construct();
      }

      public function insert($data) {
         if ($this->db->insert("stud", $data)) {
            return true;
         }
       //use try/catch, avoid only ifs with returns
      }

      public function delete($roll_no) {
         if ($this->db->delete("stud", "roll_no = ".$roll_no)) {
            return true;
         }
       //use try/catch, avoid only ifs with returns
      }

      public function update($data,$old_roll_no) {
         $this->db->set($data);
         $this->db->where("roll_no", $old_roll_no);
         $this->db->update("stud", $data);
      }
   }
?>

<!DOCTYPE html>
<html lang = "en">

   <head>
      <meta charset = "utf-8">
      <title>Students Example</title>
   </head>

   <body>

         <?php
            echo form_open('stud/add_student'); //check this route
            echo form_label('Roll No.');
            echo form_input(array('id'=>'roll_no','name'=>'roll_no'));
            echo "<br/>";

            echo form_label('Name');
            echo form_input(array('id'=>'name','name'=>'name'));
            echo "<br/>";

            echo form_submit(array('id'=>'submit','value'=>'Add'));
            echo form_close();
         ?>
   </body>

</html>

关于php - Codeigniter:无法将记录添加到数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44103138/

10-09 18:09
查看更多