dousilie9522 2013-11-06 00:56
浏览 76
已采纳

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条回答 默认 最新

  • duanmei4362 2013-11-06 11:53
    关注

    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');
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?