简体   繁体   English

CODEIGNITER-使用复选框更新多行

[英]CODEIGNITER - Updating multiple rows using checkbox

i have been trying without success to update multiple rows but each time i try i end up getting just a single row updated 我一直在尝试更新多行而没有成功,但是每次尝试时,我最终只更新了一行

CONTROLLER 控制器

public function update()
{
    $data['msg'] = '';
    $id = $_POST['reg_id'];
    $data['students'] = $this->test_model->getStudentData();
    if (isset($_POST['submit'])) {

            if (empty($id) || $id==0) {
                $data['msg'] = 'empty';
            } else {
                $userId = "'".implode("', ' ", $id)."'";

                $class =$this->input->post('class');


                $this->test_model->update($userId, $class);


            }


        } 
     $this->load->view('test', $data);
}

MODEL 模型

public function update($where, $data){
    $this->db->query('UPDATE table SET data="'.$data.'" WHERE where IN('.$where.')');

 }

view 视图

Sorry guys! 对不起大家! i have been busy and just had the chance to go through your replies. 我一直很忙,只是有机会仔细阅读您的回复。

Here is the Form view 这是表单视图

i have tried out all the solutions i got from you guys but i have not been able to get what i want. 我已经尝试了我从你们那里得到的所有解决方案,但我一直无法得到我想要的。 perhaps posting the form view would help you guys proffer a better solution 也许发布表单视图会帮助你们提供更好的解决方案

<form action="<?php echo base_url() ?>test/update" method="POST" >
    <select name="class" id="">
        <option value="some value">some value</option>
        <option value="some value">some value</option>
        <option value="some value">some value</option>
        <option value="some value">some value</option>
        <option value="some value">some value</option>
    </select>
    <input type="submit" name="submit" value="submit">
    <table border="1">
        <thead>
            <tr>
                <th>REG. ID</th>
                <th>SURNAME</th>
                <th>FIRST NAME</th>
                <th>MIDDLE NAME</th>
                <th>CLASS</th>
            </tr>
        </thead>
        <tbody>

    <?php foreach ($students as $key): ?>
        <tr>
            <td><input type="checkbox" name="reg_id[]" value="<?php echo $key->reg_id ?>"><?php echo $key->reg_id ?></td>
            <td><?php echo $key->first_name ?></td>
            <td><?php echo $key->last_name ?></td>
            <td><?php echo $key->mid_name ?></td>
            <td><?php echo $key->class ?></td>
        </tr>
    <?php endforeach ?>
        </tbody>
    </table><br>


</form>

I'm making some assumptions about what you want to achieve but, if I understand correctly, I think this is what you're looking for. 我正在对您要实现的目标进行一些假设,但是,如果我理解正确,我认为这就是您正在寻找的目标。

controller 控制者

public function update()
{
    $data['msg'] = 'empty';
    $data['students'] = $this->test_model->getStudentData();

    if(!isset($_POST['submit']))
    {
        $id_list = $this->input->post('reg_id');
        if($id_list) //were any checkboxes posted?
        {
            $data['msg'] = '';
            $class = $this->input->post('class');
            if($class)  //make sure it was posted
            {
                $this->test_model->update($id_list, $class);
            }
        }
    }
    $this->load->view('test', $data);
}

The model uses update_batch() 该模型使用update_batch()

Model 模型

public function update($id_list, $data)
{
    foreach($id_list as $id)
    {
        $update_data[] = ['where' => $id, 'data' => $data,];
    }

    if(isset($update_data))
    {
        $this->db->update_batch('table', $update_data, 'where');
    }
}

In your query you missed to pass the column name in the query. 在您的查询中,您没有在查询中传递列名。 When you use IN keyword then the database column name must required before IN keyword. 使用IN关键字时,必须在IN关键字之前要求数据库列名称。

For Example: UPDATE table_name SET name='abcd' WHERE id IN('2,3,5'); 例如:UPDATE table_name SET name ='abcd'WHERE id IN('2,3,5');

This will update the record number 2, 3 and 5. 这将更新记录编号2、3和5。

Try the below code in your model. 在模型中尝试以下代码。 Add your database column name in the query. 在查询中添加数据库列名称。

public function update($where, $data){
$this->db->query('UPDATE table SET data="'.$data.'" WHERE where id IN('.$where.')'); 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM