繁体   English   中英

mysql / php / codeigniter更新具有相同ID的多行

[英]mysql/php/codeigniter Update multiple rows with the same id

我有这两个表:

questions            answers
+-----+---------+    +------+------+---------+
| id_q| question|    | id_q | id_a | answer  |
+=====+=========+    +======+======+=========+
|  1  |question1|    |   1  |   1  | answer1 |
+-----+---------+    +------+------+---------+
|  2  |question2|    |   1  |   2  | answer2 |
+-----+---------+    +------+------+---------+
                     |   1  |   3  | answer3 |
                     +------+------+---------+
                     |   1  |   4  | answer4 |
                     +------+------+---------+
                     |   2  |   5  | answer5 |
                     +------+------+---------+
                     |   2  |   6  | answer6 |
                     +------+------+---------+
                     |   2  |   7  | answer7 |
                     +------+------+---------+
                     |   2  |   8  | answer8 |
                     +------+------+---------+

我正在尝试制作一个更新表格,可以在其中更新每个问题的每个答案。 下面表格的图片(顶部的数字是选择的id_q)。

目前我所拥有的是:

this->db->SELECT('*');
this->db->FROM('answers');
this->db->WHERE('answers.id_q', $x); // $x -> variable that has the id o the selected question
this->db->UPDATE('answers', $data);

显然,这是在更新表上所有具有相同id_q的条目,结果是:

answers
+------+------+---------+
| id_q | id_a | answer  |
+======+======+=========+
|   1  |   1  | answer1 |
+------+------+---------+
|   1  |   2  | answer1 |
+------+------+---------+
|   1  |   3  | answer1 |
+------+------+---------+
|   1  |   4  | answer1 |
+------+------+---------+

我要结束的是:能够更新每个问题的每个答案。 已经尝试使用临时表,但没有成功。

编辑:我期望得到的是当我更新这样的答案时:

https://imgur.com/a/SPg81IA

要获得这样的数据库:

answers
+------+------+-------------+
| id_q | id_a |   answer    |
+======+======+=============+
|   1  |   1  | answerone   |
+------+------+-------------+
|   1  |   2  | answertwo   |
+------+------+-------------+
|   1  |   3  | answerthree |
+------+------+-------------+
|   1  |   4  | answerfour  |
+------+------+-------------+

但是目前,我用“ answerone”获得了所有四个字段。

据我了解您的问题。 我想你要

this->db->SELECT('*');
this->db->FROM('answers');
this->db->WHERE('answers.id_q', $x); // $x -> variable that has the id o the selected question
this->db->WHERE('answers.id_a', $rightAnswerId);
this->db->UPDATE('answers', $data);

首先在您的更新表格中

在视图文件中:

对于输入字段answer1,answer2等。

将输入字段的名称放在数组中:

<input type="text" name="answers[]" placeholder="Answer 1"/>
<input type="text" name="answers[]" placeholder="Answer 2"/>
<input type="text" name="answers[]" placeholder="Answer 3"/>
<input type="text" name="answers[]" placeholder="Answer 4"/>

在控制器端:您可以将这些字段捕获为:

$answers = $this->input->post('answers');
$update_data = array();
for($answers as $key => $value){
  $update_data['answer'] = $value;
  //This part now should be done in model side:
  $this->db->where('id_q',$id_q);
  $this->db->where('id_a',($key + 1));
  $this->db->update('answers', $update_data);
}

更新问题:

this->db->UPDATE('questions', array('question' => 'question_title'),array('id_q' => 1));

更新答案:-

首先删除问题ID 1的所有答案

$this->db->delete('answers',array('id_q' => 1));

然后重新插入问题编号为1的所有答案

<?php 

$answers = $this->input->post('answers');
$insert_data = array();
for($answers as $key => $value){
  $row['id_q'] = 1;
  $row['answer'] = $value;
  array_push($insert_data, $row);
}
if(!empty($insert_data)){
    $this->db->insert_batch('answers',$insert_data); // will insert multiple rows
}

?>

这将工作100%。

暂无
暂无

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

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