简体   繁体   English

如何将表1中的行与表2中的数据连接,其中表2中的所有数据都具有表1中的行ID

[英]How to connect row from table1 with data in table2 where all data in table2 has id of row from table1

//Table 1
$table = "CREATE TABLE IF NOT EXISTS questions (
id INT (11) NOT NULL AUTO_INCREMENT,
quesion TEXT NOT NULL,
user VARCHAR (255) NOT NULL,
date TIMESTAMP NOT NULL,
question TEXT NOT NULL,
answer TEXT NOT NULL,
PRIMARY KEY (id)
)";

//Table 2
$table = "CREATE TABLE IF NOT EXISTS answers (
id INT (11) NOT NULL AUTO_INCREMENT,
user VARCHAR (255) NOT NULL,
answer TEXT NOT NULL,
datum TIMESTAMP NOT NULL,
PRIMARY KEY (id)
)";

I have two forms. 我有两种形式。 One for inserting question and other for inserting answers. 一个用于插入问题,另一个用于插入答案。 It's working. 工作正常 But i have a problem. 但是我有一个问题。 It is normal that for one question you have few answers, but right now when i submit answer, and try to add second answer for first question, it overwrite me first answer. 对于一个问题,您几乎没有答案是正常的,但是现在当我提交答案,并尝试为第一个问题添加第二个答案时,它会覆盖我的第一个答案。 //this code do that //这段代码可以做到

$sql="UPDATE questions SET answer='$_POST[answer]' WHERE id='$_POST[id]'";

I now - it is obviously that every inserted answer do to the table 'questions', but i need to insert answers into table 'answers' and every answer to be connected for his question from table 'question' 我现在-很显然,每个插入的答案都会对表“ questions”起作用,但是我需要将答案插入到表“ answers”中,并且每个答案都必须与表“ question”中的问题相关联

So, resume - user come to the site, see The question, answer on it and leave, after come second user and give answer for same question and also leave.. How to make it work. 因此,简历-用户来到该站点,请参阅问题,然后在该问题上回答然后离开,第二位用户在对相同问题给出答案后也离开..如何使其正常工作。 I've trying with some foreign keys, but nothing.. 我正在尝试使用一些外键,但是什么也没有。

Add an extra field, lets say for ex. 添加一个额外的字段,比如说。 QuestionId to your answser table. 您的回答表的QuestionId In your insert SQL u provide the QuestionId to your answser insert statement. 在您的插入SQL中,向您的answser插入语句提供QuestionId。

Then u can select all your answers for one question with : 然后,您可以使用以下命令为一个问题选择所有答案:

SELECT * FROM answser WHERE QuestionId=$qId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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