0

**

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

**

When I try to run the sql query, it works on a sql interpreter, DBeaver. However, PHP gives the error.

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 part

$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;
}
2
  • 1
    Stop checking for mysqli errors manually! Enable error reporting instead. How to get the error message in MySQLi? It will make your life much easier and your code safer. Commented Oct 25, 2020 at 19:23
  • 1
    @Dharman Thank you for the advice . I don't know php in a good manner. However, I'll heed in next rounds. Commented Oct 25, 2020 at 19:41

2 Answers 2

0

mysqli::query() does not support running multiple statements - you would need mysqli::multi_query() instead.

But here, for this simple insert passing literal values, I would recommend the following syntax, which is a single statement:

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

Note that I modified your code by removing the single quotes around the numbers; presumably, the corresponding columns have int datatype: if so, it is simpler and more efficient to pass the values as literal numbers.

Sign up to request clarification or add additional context in comments.

Comments

0

This is only fpr 1 qiery.

If you want to insert them this way you need multi_query

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

Then you can also make,l this as one 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');

And then you can use prepared statements with parameters multiple times

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.