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