duancong6937 2018-11-19 12:04
浏览 25

mysql / php / codeigniter更新具有相同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'.

  • 写回答

3条回答 默认 最新

  • 普通网友 2018-11-19 12:22
    关注

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

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了