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

时间:2015-07-03 06:53:12

标签: php codeigniter activerecord

我只想问一下如何在一个查询中更新多行。我只能使用insert_batch函数连续插入多个数据,但我发现它无法在更新批处理中使用。我不知道怎么做。我已经在google上搜索了如何制作但我没有运气。非常感谢您的回答。

我有两张表'问卷''选项'

表格信息。 问卷

  

-questionnaire_id

     

-type_id_fk

     

-question

     

-answer

表格信息。 选择

  

-choices_id

     

-questionnaire_id_fk

     

-choices

我的控制器

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

我的模特

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

    }

我的观点

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

1 个答案:

答案 0 :(得分:0)

对于多次更新,您可以使用$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');