Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL最后插入ID语法_Php_Mysql_Last Insert Id - Fatal编程技术网

Php MySQL最后插入ID语法

Php MySQL最后插入ID语法,php,mysql,last-insert-id,Php,Mysql,Last Insert Id,我有以下代码: //insert user input into db $query = "INSERT INTO test_details (test_title, user_id, likes) VALUES ('$title', '$user_id', '0')"; $query .= "INSERT INTO test_descriptions (test_id, description) VALUES (LAST_INSERT_ID(), '$description')"; if(is

我有以下代码:

//insert user input into db
$query = "INSERT INTO test_details (test_title, user_id, likes)
VALUES ('$title', '$user_id', '0')";
$query .= "INSERT INTO test_descriptions (test_id, description)
VALUES (LAST_INSERT_ID(), '$description')";
if(isset($grade) && isset($difficulty) && isset($subject)) {
    $query .= "INSERT INTO test_filters (test_id, grade, subject, difficulty)
    VALUES (LAST_INSERT_ID(), '$grade', '$subject', '$difficulty')";
}
if(mysqli_multi_query($con, $query)) {
    echo 'Go <a href="../create">back</a> to start creating questions.';
}
else {
    echo "An error occurred! Try again later.";
    echo mysqli_error($con);
}
//将用户输入插入数据库
$query=“插入测试详细信息(测试标题、用户id、喜好)
值(“$title”、“$user_id”、“0”)”;
$query.=“插入测试描述(测试id,描述)
值(最后插入的ID(),“$description”)”;
if(isset($成绩)和isset($难度)和isset($科目)){
$query.=“插入测试过滤器(测试id、等级、科目、难度)
值(最后一个插入的ID(),'$grade','$subject','$CONDIUSION')”;
}
if(mysqli_多_查询($con,$query)){
echo“开始创建问题”;
}
否则{
echo“发生错误!请稍后再试。”;
echo mysqli_错误($con);
}

当我尝试执行代码时,我收到了这个MySQL错误:
您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在“SET@id=(SELECT LAST_INSERT_id())INSERT INTO test_description(test_id,descr)”(第2行)附近使用的正确语法。不确定哪里出错了,所有语法似乎都正确。谢谢。

您的mutli查询语句中缺少分号

您可以将它们添加到要连接的查询前面(
=
),以保持一致性,因为if语句可能会也可能不会将查询添加到混合中

//insert user input into db
$query = "INSERT INTO test_details (test_title, user_id, likes)
VALUES ('$title', '$user_id', '0')";
$query .= ";INSERT INTO test_descriptions (test_id, description)
VALUES (LAST_INSERT_ID(), '$description')";
if(isset($grade) && isset($difficulty) && isset($subject)) {
    $query .= ";INSERT INTO test_descriptions (test_id, grade, subject, difficulty)
    VALUES (LAST_INSERT_ID(), '$grade', '$subject', '$difficulty')";
}
if(mysqli_multi_query($con, $query)) {
    echo 'Go <a href="../create">back</a> to start creating questions.';
}
else {
    echo "An error occurred! Try again later.";
    echo mysqli_error($con);
}
//将用户输入插入数据库
$query=“插入测试详细信息(测试标题、用户id、喜好)
值(“$title”、“$user_id”、“0”)”;
$query.=“插入测试描述(测试id,描述)
值(最后插入的ID(),“$description”)”;
if(isset($成绩)和isset($难度)和isset($科目)){
$query.=“插入测试描述(测试id、等级、科目、难度)
值(最后一个插入的ID(),'$grade','$subject','$CONDIUSION')”;
}
if(mysqli_多_查询($con,$query)){
echo“开始创建问题”;
}
否则{
echo“发生错误!请稍后再试。”;
echo mysqli_错误($con);
}
或者正如andrewsi提到的,内爆方法:

//insert user input into db
$query[] = "INSERT INTO test_details (test_title, user_id, likes)
VALUES ('$title', '$user_id', '0')";
$query[] = "INSERT INTO test_descriptions (test_id, description)
VALUES (LAST_INSERT_ID(), '$description')";
if(isset($grade) && isset($difficulty) && isset($subject)) {
    $query[] = "INSERT INTO test_descriptions (test_id, grade, subject, difficulty)
    VALUES (LAST_INSERT_ID(), '$grade', '$subject', '$difficulty')";
}
if(mysqli_multi_query($con, implode( ';', $query ))) {
    echo 'Go <a href="../create">back</a> to start creating questions.';
}
else {
    echo "An error occurred! Try again later.";
    echo mysqli_error($con);
}
//将用户输入插入数据库
$query[]=“插入测试详细信息(测试标题、用户id、喜好)
值(“$title”、“$user_id”、“0”)”;
$query[]=“插入测试描述(测试id,描述)
值(最后插入的ID(),“$description”)”;
if(isset($成绩)和isset($难度)和isset($科目)){
$query[]=“插入测试描述(测试id、等级、科目、难度)
值(最后一个插入的ID(),'$grade','$subject','$CONDIUSION')”;
}
if(mysqli_multi_query($con,introde(“;”,$query))){
echo“开始创建问题”;
}
否则{
echo“发生错误!请稍后再试。”;
echo mysqli_错误($con);
}

您需要在每个单独的SQL语句之间添加分号。错误消息包括代码摘录中未出现的SQL片段(
SET@id…
)。是我遗漏了什么还是你遗漏了什么?你也可以将单独的语句添加到数组中,然后
使用分号作为粘合剂将其内爆。
谢谢你提供的两个解决方案,我一直习惯于使用mysqli_查询。将接受正确答案。另外,为什么要在查询前添加分号?代码将在是的。我想这是我的强迫症。我需要好好想想,可以用它来结束每一个查询。我想用最少的字符来保持一致性。