PHP Codeigniter使用提交按钮插入删除更新

时间:2013-11-06 00:56:33

标签: php codeigniter codeigniter-datamapper

需要 PHP Codeigniter框架的帮助。

我有一个包含3个提交按钮的表单。我想插入 - 更新 - 使用这些按钮删除。我成功地使“插入”按钮有效。所以当我点击Insert时,数据存储在MySQL Database表中。因为它们都是提交按钮,所以它们都在做同样的动作。

当我更改类型输入类型=“按钮”不起作用时。

下面是我的PHP代码。

模型

class Users_model extends CI_Model

{

   function get_All_Users(){

        $query = $this->db->get('users');
        if($query->num_rows() > 0){
            foreach($query->result() as $row) {
                $data[]=$row;
            }
        }
        return $data;
    }


    function insert_users($data){
        $query = $this->db->insert('users', $data);
        return;


    }

    function update_users($data){
        // update code...
    }
}

控制器

public function add_users(){

    $this->load->view('users_view');
}

    public function create_users(){

        $username = $this->input->post('username');
        $password = $this->input->post('password');


        $data = array(
            'username' => $username,
            'password' => $password,
        );

        $this->load->model('user_model');
        $this->users_model->insert_users($data);
        $this->load->view('users_view');
        redirect('users', 'refresh');

    }

查看

<form role="form" action="<?php echo base_url().'index.php/users/create_users'?>" method="post">
    <table class="table">
        <thead>

        </thead>
        <tbody>
        <tr>
            <td>
                <div class="form-group">
                    <label for="Username">Username</label>
                    <input type="textbox" id="username" name="username">
                </div>
            </td>

            <td>
                <div class="form-group">
                    <label for="Password">Password</label>
                    <input type="textbox" id="password" name="password">
                </div>
            </td>     
        </tr>

        <tr>
            <td>
                <input type="submit" value="Insert" />
                <input type="submit" value="Update" />
                <input type="submit" value="Delete" />
            </td>
        </tr>
        </tbody>
    </table>

    </form>

所以我想点击更新按钮运行功能更新!

3 个答案:

答案 0 :(得分:1)

首先给按钮命名

<input type="submit" name="insert" value="Insert" />
<input type="submit" name="update" value="Update" />
<input type="submit" name="delete" value="Delete" />

并尝试以下功能

public function create_users(){
    $this->load->model('user_model');
if($this->input->post('insert') != ''){
/*Perform insert operation here*/

    $username = $this->input->post('username');
    $password = $this->input->post('password');


    $data = array(
        'username' => $username,
        'password' => $password,
    );


    $this->users_model->insert_users($data);
}
else if($this->input->post('update') != ''){
     /*perform update operation here*/
}
else if($this->input->post('delete') != ''){
     /*perform delete operation here*/
}
    $this->load->view('users_view');
    redirect('users', 'refresh');

}

答案 1 :(得分:0)

$this->load->model('user_model');

您未在此处加载正确的型号。将其更改为$ this-&gt; load-&gt; model('users_model');

答案 2 :(得分:-1)

you type wrong this code area

  wrong:
       $data = array(
        'username' => $username,
        'password' => $password,
    );

  right
      you can write one extra semicolon in password field
  $data = array(
        'username' => $username,
        'password' => $password
    );