简体   繁体   English

如何使用Codeigniter在一个查询中更新数据库中的多行

[英]How to update multiple rows from database in one query using codeigniter

I just want to ask how to update multiple rows in one query. 我只想问一下如何在一个查询中更新多行。 I can only insert multiple data in a row using the insert_batch function but I couldn't find it works in update batch. 我只能使用insert_batch函数将多个数据连续插入,但找不到在更新批处理中有效的数据。 I don't know how to do it. 我不知道该怎么做。 I have already searched on google on how to make it but I have no luck. 我已经在Google上搜索了制作方法,但是我没有运气。 Your answer is very much appreciated. 非常感谢您的回答。

I have two tables 'questionnaire' and 'choices' 我有两个表“问卷”“选择”

table info. 表信息 for questionnaire table 用于问卷

-questionnaire_id -questionnaire_id

-type_id_fk -type_id_fk

-question -题

-answer -回答

table info. 表信息 for choices table 选择

-choices_id -choices_id

-questionnaire_id_fk -questionnaire_id_fk

-choices -choices

My Controller 我的控制器

public function update_selectedQuestionnaire() {
      $this->test_model->update_questionnaire();
      redirect('test/questionnaire', 'refresh');
}

My Model 我的模特

    function update_questionnaire() {

            $Qid = $this->input->post('Qid'); //questionnaire id
            $Cid = $this->input->post('Cid'); //questionnaire id
            $type = $this->input->post('type');
            $question = $this->db->escape_like_str($this->input->post('question'));
            $answer = $this->db->escape_like_str($this->input->post('answer'));

            $choice1 = $this->db->escape_like_str($this->input->post('choice1'));
            $choice2 = $this->db->escape_like_str($this->input->post('choice2'));
            $choice3 = $this->db->escape_like_str($this->input->post('choice3'));
            $choice4 = $this->db->escape_like_str($this->input->post('choice4'));


       $Cdata = array(
            array(
                'choices_id' => $Cid,
                'questionnaire_id_fk' => $Qid,
                'choices' => $choice1
            ),
            array(
                'choices_id' => $Cid,
                'questionnaire_id_fk' => $Qid,
                'choices' => $choice2
            ),
            array(
                'choices_id' => $Cid,
                'questionnaire_id_fk' => $Qid,
                'choices' => $choice3
            ),
            array(
                'choices_id' => $Cid,
                'questionnaire_id_fk' => $Qid,
                'choices' => $choice4
            )
        );

        $this->db->trans_start();
        $this->db->query("UPDATE questionnaire SET type_id_fk=$type, question='$question', answer='$answer' WHERE questionnaire_id=$Qid");
        $this->db->update_batch('choices', $Cdata, 'choices_id');
        var_dump($Cdata);
        $this->db->trans_complete();

    }

My view 我的观点

<div class="jumbotron">
        <div class=" col-md-9 col-sm-12 col-xs-12 panel-body">
            <h3>Update Questionnaire</h3>
            <?php echo form_open('test/update_selectedQuestionnaire'); ?>
            <?php
            foreach ($query as $row): echo '<input type="hidden" name="Qid" id="Qid" value="' . $row->questionnaire_id . '"/><br />';
            endforeach;
            foreach ($choices as $row): echo '<input type="hidden" name="Cid" id="Cid" value="' . $row->choices_id . '"/>';
            endforeach;
            ?>
            <div class="col-md-2 col-sm-2"><label>Type:</label></div><div class="col-md-10">
                <?php
                echo '<select name="type" id="type" required class="form-control">';
                echo '<option value=""></option>';
                foreach ($type as $tRow):
                    echo '<option value="' . $tRow->type_id . '"';
                    foreach ($query as $qRow):
                        if ($tRow->type_id === $qRow->type_id_fk):
                            echo 'selected';
                        endif;
                    endforeach;
                    echo '>' . $tRow->type . '</option>';
                endforeach;
                echo '</select>';
                ?>
            </div>
            <div class="col-md-2 col-sm-2"><label>Question:</label></div><div class="col-md-10">
                <?php
                foreach ($query as $row): echo '<input type="text" name="question" id="question" value="' . $row->question . '" required class="form-control"/>';
                endforeach;
                ?>
            </div>
            <div class="jumbotron">

                <?php
                $i = 0;
                foreach ($choices as $row): $i++;
                    echo '<div class="col-md-2 col-sm-2"><label>Choice ' . $i . ':</label></div><div class="col-md-9"><div class="col-md-11 col-sm-11 col-xs-11">'
                    . '<input type="text" name="choices" id="choice_' . $row->choices_id . '" value="' . $row->choices . '" class="form-control"/>'
                    . '</div><div class="col-md-1 col-sm-1 col-xs-1"><a class="label label-warning" hef="#" id="setAns_' . $row->choices_id . '">set</a></div></div>';
                endforeach;
                ?>
                <div class="col-md-2 col-sm-2"><label>Answer:</label></div><div class="col-md-10">
                    <?php
                    foreach ($query as $row): echo '<input type="text" name="answer" id="answer" value="' . $row->answer . '" required class="form-control" readonly/>';
                    endforeach;
                    ?>
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="text-center col-md-9 col-sm-12 col-xs-12">
                <hr />
                <button type="button" onclick="goBack()" class="btn btn-default">Cancel</button>
                <button type="submit" name="update" class="btn btn-primary">Update</button>
            </div>
        </div>
        <?php form_close(); ?>
    </div>

For multiple update you can use $this->db->update_batch 对于多次更新,您可以使用$this->db->update_batch

     $Cdata = array(
                array(
                    'choices_id' => $Cid,
                    'questionnaire_id_fk' => $Qid,
                    'choices' => $choice1
                ),
                array(
                    'choices_id' => $Cid,
                    'questionnaire_id_fk' => $Qid,
                    'choices' => $choice2
                ),
                array(
                    'choices_id' => $Cid,
                    'questionnaire_id_fk' => $Qid,
                    'choices' => $choice3
                ),
                array(
                    'choices_id' => $Cid,
                    'questionnaire_id_fk' => $Qid,
                    'choices' => $choice4
                )
            );

$this->db->update_batch('choices', $Cdata, 'choices_id'); 

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

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