如何在代码点火器中更新表单数据


how to update form data in codeigniter

我是codeigniter的新手。

我正在为这个项目使用编码点火器。我不知道如何更新数据库中的表单数据。我已经插入,在数据库中显示数据已完成。但是我不明白,如何更新数据库中的数据。

我的控制器:

    class User extends CI_Controller {
        public function __construct() {
            // Call the Model constructor
            parent::__construct();
            $this->load->model('usermodel');
        }
        public function insert() {
            $this->load->view('userview');
            if ($this->input->post('submit')) {
                $this->usermodel->save();
            }
        }
        public function display() {
            $data = array();
            $data['result'] = $this->usermodel->get_contents();
            $this->load->view('usergrid', $data);
        }
        public function edit() {
            $data = array();
            $get = $this->uri->uri_to_assoc();
            $data['result'] = $this->usermodel->entry_update( $get['id'] );
            $this->load->view('useredit', $data);
             if ($this->input->post('submit')) {
                $this->usermodel->entry_update1($get['id']);
            }
        }

    }

型:

        <?php
        class Usermodel extends CI_Model {
            public function __construct() {
                // Call the Model constructor
                parent::__construct();
            }
            public function save() {
                //print_r($this->input->post('name'));
                $data = array(
                    'name' => $this->input->post('name'),
                    'age' => $this->input->post('age'),
                    'address' => $this->input->post('address')
                );
                //var_dump($this->db);
                $this->db->insert('user', $data);
            }
            public function get_contents() {
                $this->db->select('*');
                $this->db->from('user');
                $query = $this->db->get();
                return $result = $query->result();
            }

            public function entry_update( $id ) {
                $this->db->select('*');
                $this->db->from('user');
                $this->db->where('id',$id );
                $query = $this->db->get();
                return $result = $query->row_array();
            }
            public function entry_update1($id) {
              $data = array(
       
        'name' => $this->input->post('name'),
        'age' => $this->input->post('age'),
        'address' => $this->input->post('address')
                );
                 $this->db->where('id', $id);
                $this->db->update('user', $data);
            }
        }
        ?>

视图:

    <html>
        <head>
            <title>user registration</title>
        </head>
        <body>
            <form  action="edit"  method="POST" name="myform">
                <input type="hidden" name="id" value="<?php echo $result['id']; ?>">
                username :<input type="text" name="name" value="<?php echo $result['name'] ?>"></br>
                age      :<input type="text" name="age" value="<?php echo $result['age'] ?>"></br>
                Address  :<input type="text" name="address" value="<?php echo $result['address'] ?>"></br>
                <input type="submit" value="update" name="submit">
            </form>
        </body>
    </html>

提前感谢您的帮助。

你只是在传递$id

$this->usermodel->entry_update1($get['id']);

在函数中你做了

public function entry_update1($id) {
    $this->db->where('id', $id);
    $this->db->update('user', $data);
}

所以你也必须在你的函数调用中传递$data

$this->usermodel->entry_update1($get['id'], $data);

public function entry_update1($id, $data) {
    $this->db->where('id', $id);
    $this->db->update('user', $data);
}

只需快速浏览一下,我就可以看到您没有向entry_update1函数中的$data传递任何内容;

public function entry_update1($id) {
    $this->db->where('id', $id);
    $this->db->update('user', $data);
}

您尝试使用 $data 更新"用户",但尚未设置$data。

您必须在 CodeIgniter 中以这种方式运行更新查询。

        public function entry_update1($id,$data) {
            $this->db->set($data);
            $this->db->where('id',$id);
            $update = $this->db->update('user');
            if($update)
            {
                return true;
            }
            else
            {
               return false;
            }
         }