【问题标题】:How to update multiple records at the same time?如何同时更新多条记录?
【发布时间】:2020-03-29 06:21:33
【问题描述】:

我想在代码点火器中更新多个具有相同 ID 的行。只有第一个数据正在更新。我要更新多个数据

控制器

  public function edit_curriculum_details($id) {

        if (isset($_POST['submit'])) {
            $post = $this->input->post();

             $param = array(
                'course_id' => $post['course_id'],
                'introduction' => $post['introduction_curriculum'],
                'type' => $post['type'],
                'question' => $post['question_curriculum'],
                'answer' => $post['answer_curriculum'],
            );

            $where = array('course_id' => $id);
            $data['add'] = $this->Model->update_data('tbl_common', $param, $where);
}

型号 函数 update_data($table, $param, $where) {

    $this->db->where($where);

    $this->db->update($table, $param);

    return true;
}

查看

<input  class="form-control" name="question_curriculum" value="<?php echo $course['question']; ?>

数据库

+-----------+--------------+---------------------+---------+------------+-----------+
| common_id | introduction | question            | answer  | type       | course_id |
+-----------+--------------+---------------------+---------+------------+-----------+
| 64        | curriculum   | Question curriculum | answer1 | curriculum | 29        |
+-----------+--------------+---------------------+---------+------------+-----------+
| 65        | curriculun   | Question curriculum | answer1 | curriculum | 29        |
+-----------+--------------+---------------------+---------+------------+-----------+

【问题讨论】:

  • update_data() 函数中有什么?为什么要在 $param 数组中添加“course_id”?
  • 同时添加 Model 型号代码。
  • 可能有很多课程问答。所以我需要选择否。问答然后更新。现在只获取一个第一行记录。例如。 id 64.Array ( [common_id] => 64 [介绍] => 课程 [问题] => 问题课程 How to Answer =>answer1 [类型] => 课程 [course_id] => 29)

标签: mysql codeigniter


【解决方案1】:

您可以尝试在模型中使用update_batch() 方法。有关如何准备数据的更多详细信息,请查看文档:https://codeigniter.com/user_guide/database/query_builder.html#updating-data

【讨论】:

    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2019-02-18
    • 1970-01-01
    • 2016-05-01
    • 2013-05-02
    • 2014-03-04
    相关资源
    最近更新 更多