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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19802081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:02:56  来源:igfitidea点击:

PHP Codeigniter insert delete update with submit buttons

phpcodeignitercodeigniter-datamapper

提问by Barlet

Need help with PHP Codeigniter framework.

需要有关PHP Codeigniter 框架的帮助。

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.

我有一个带有 3 个提交按钮的表单。我想使用这些按钮插入 - 更新 - 删除。我成功地使插入按钮工作。因此,当我单击插入时,数据存储在 MySQL 数据库表中。由于它们都是提交按钮,因此它们都在执行相同的操作。

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

当我更改类型 input type="button" 不起作用。

below is my php code.

下面是我的php代码。

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 updatebutton to run function update!

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

回答by Vamsi

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

}

回答by alfiebesin

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

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

您没有在此处加载正确的模型。将其更改为 $this->load->model('users_model');

回答by piyush vavaliya

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