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

Need help with PHP Codeigniter framework.

I have a form with 3 Submit buttons. I want to Insert - Update - Delete using those buttons. I successfully made the Insert button work. So when I click on Insert the data is stored in MySQL Database table. Since they are all submit buttons all of them are doing the same action.

when I change type input type="button" is not working.

below is my php code.

Model

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...
    }
}

Controller

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');

    }

View

<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>

So I want when I click on update button to run function update!

3个回答

Firstly give names to buttons

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

and try the following function

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');

}
doutongxuan1614
doutongxuan1614 谢谢......它的工作完美......
接近 7 年之前 回复
$this->load->model('user_model');

You're not loading the right model here. Change it to $this->load->model('users_model');

dphe5602
dphe5602 也许我在这里键入错误...代码正在运行。 我需要的是如何告诉第二个提交按钮运行另一个功能
接近 7 年之前 回复
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
    );
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐