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

时间:2018-11-19 12:04:01

标签: php mysql codeigniter

我有这两个表:

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”获得了所有四个字段。

3 个答案:

答案 0 :(得分:0)

据我了解,您的问题正确无误。我想你要

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

答案 1 :(得分:0)

首先在您的更新表单中

在查看文件中:

对于输入字段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);
}

答案 2 :(得分:0)

要更新问题:-

http://myServer/reports/report/Purchasing/Copy%20of%20List%20Purchase%20Orders

更新答案:-

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

http://myServer/ReportServer/Pages/ReportViewer.aspx?/Purchasing/Copy%20of%20List%20Purchase%20Orders

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

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

这将100%起作用。