Mysql 插入给出错误而 Sql 编辑器没有

Mysql insert gives error while Sql editor doesn't

**

(: What's wrong with the question to give -1 ? Please briefly leave its reason.

**

当我尝试 运行 sql 查询时,它在 sql 解释器 DBeaver 上运行。但是,PHP 给出了错误。

Error: INSERT INTO TestSistem.Attendance (DayDate,Presence,StudentId,TeacherId) VALUES ('2020-10-25 21:09:57', '1' ,'4' ,'3'); INSERT INTO TestSistem.Attendance (DayDate,Presence,StudentId,TeacherId) VALUES ('2020-10-25 21:09:57', 0 ,'3' ,'3');
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO TestSistem.Attendance (DayDate,Presence,StudentId,TeacherId) VALUES ' at line 1

Php部分

$sql = "INSERT INTO TestSistem.Attendance (DayDate,Presence,StudentId,TeacherId) VALUES ('2020-10-25 21:09:57', '1' ,'4' ,'3'); INSERT INTO TestSistem.Attendance (DayDate,Presence,StudentId,TeacherId) VALUES ('2020-10-25 21:09:57', '0' ,'3' ,'3');";
if ($mysqli->query($sql) === TRUE) {
    echo "New record(s) created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $mysqli->error;
}

mysqli::query() does not support running multiple statements - you would need mysqli::multi_query() 代替。

但是在这里,对于这个简单的插入传递文字值,我建议使用以下语法,它是一个语句:

INSERT INTO TestSistem.Attendance (DayDate,Presence,StudentId,TeacherId) 
VALUES 
    ('2020-10-25 21:09:57', 1 , 4 , 3),
    ('2020-10-25 21:09:57', 0 , 3 , 3);

请注意,我通过删除数字周围的单引号修改了您的代码;据推测,相应的列具有 int 数据类型:如果是这样,将值作为文字数字传递会更简单、更有效。

这只是fpr 1 qiery

如果你想以这种方式插入它们,你需要 multi_query

https://www.php.net/manual/en/mysqli.multi-query.php

那你也可以把这个作为一个查询

INSERT INTO TestSistem.Attendance (DayDate,Presence,StudentId,TeacherId) 
VALUES 
    ('2020-10-25 21:09:57', '1' ,'4' ,'3'),
    ('2020-10-25 21:09:57', 0 ,'3' ,'3');

然后你可以多次使用带参数的预处理语句

https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php