简体   繁体   中英

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

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:

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

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'.

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.

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

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

then re-insert all answers with question id 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%.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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