[英]mysql/php/codeigniter Update multiple rows with the same id
I have these 2 tables: 我有这两个表:
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 |
+------+------+---------+
And I'm trying to make an update form where i could update each answer of each question. 我正在尝试制作一个更新表格,可以在其中更新每个问题的每个答案。 Picture of the form below (the number on the top is the id_q selected).
下面表格的图片(顶部的数字是选择的id_q)。
At the moment what I have is: 目前我所拥有的是:
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);
Which, obviously, what is doing is updating all the entries on the table that have the same id_q, resulting in something like: 显然,这是在更新表上所有具有相同id_q的条目,结果是:
answers
+------+------+---------+
| id_q | id_a | answer |
+======+======+=========+
| 1 | 1 | answer1 |
+------+------+---------+
| 1 | 2 | answer1 |
+------+------+---------+
| 1 | 3 | answer1 |
+------+------+---------+
| 1 | 4 | answer1 |
+------+------+---------+
What I would like to end up with is: being able to update each answer of each question. 我要结束的是:能够更新每个问题的每个答案。 Already tried using temporary tables but without success.
已经尝试使用临时表,但没有成功。
EDIT: What i am expecting to get is when i update the answers like this: 编辑:我期望得到的是当我更新这样的答案时:
https://imgur.com/a/SPg81IA https://imgur.com/a/SPg81IA
To get the database like this: 要获得这样的数据库:
answers
+------+------+-------------+
| id_q | id_a | answer |
+======+======+=============+
| 1 | 1 | answerone |
+------+------+-------------+
| 1 | 2 | answertwo |
+------+------+-------------+
| 1 | 3 | answerthree |
+------+------+-------------+
| 1 | 4 | answerfour |
+------+------+-------------+
But at the moment i get all the four fields with 'answerone'. 但是目前,我用“ answerone”获得了所有四个字段。
As per I understand your question properly. 据我了解您的问题。 I think you want
我想你要
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);
First of all in your update form 首先在您的更新表格中
In view file: 在视图文件中:
For input field answer1,answer2 etc. 对于输入字段answer1,answer2等。
Put the name for input fields like this in array: 将输入字段的名称放在数组中:
<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"/>
On Controller Side: you can grab these fields as: 在控制器端:您可以将这些字段捕获为:
$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);
}
To update question:- 更新问题:
this->db->UPDATE('questions', array('question' => 'question_title'),array('id_q' => 1));
To update answers:- 更新答案:-
First delete all answers of question id 1 首先删除问题ID 1的所有答案
$this->db->delete('answers',array('id_q' => 1));
then re-insert all answers with question id 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
}
?>
This will work 100%. 这将工作100%。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.