[英]How to Add mysql foreign key from another table primary key?
What should I add as the second value?我应该添加什么作为第二个值?
I tried last id, but it is not working.我试过最后一个 id,但它不起作用。
I want to add the question's primary key.我想添加问题的主键。
$connect->exec(" INSERT INTO answers(choice_option, questions_id)
VALUES('$choiceoption', [what goes here?])");
Your code is a little bit old, maybe is Legacy code but I recommend to you that use mysql_insert_id() if primary key: questions_id is AUTO_INCREMENT.您的代码有点旧,可能是旧代码,但我建议您使用 mysql_insert_id() 如果主键:questions_id 是 AUTO_INCREMENT。 For example:
例如:
<?
$connect->exec("INSERT INTO questions
(questions_points, questions_ask, answer_option, contest_id)
VALUES ('$questionspoints', '$questionsask', '$answeroption', '$lastid') ");
//Get last inserted from question's table:
$last_insert = mysql_insert_id();
$connect->exec(" INSERT INTO answers(choice_option, questions_id)
VALUES('$choiceoption', '$last_insert')");
?>
Suggestion: Use mysqli or PDO instead classic mysql because is deprecated.
建议:使用 mysqli 或 PDO 代替经典的 mysql,因为不推荐使用。
More information: mysql_insert_id更多信息: mysql_insert_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.