Error INSERT INTO MySQL Table
Learning PHP with MYSQL from w3schools. Everything is going fine but when i am trying to enter multiple value into table and it shows below error.
Error INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Mary', 'Moe', 'mary@example.com')INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Julie', 'Dooley', 'julie@example.com')
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 MyGuests (firstname, lastname, email) VALUES ('Mary', 'Moe', 'm' at line 2
Here is my code Sample
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'envy';
// Create Connection
$conn = new mysqli($server, $username, $password, $database);
if($conn->connect_error){
die("Connected Successfully.".$conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com')";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";
if($conn->query($sql)==TRUE){
$lastid = $conn->insert_id;
echo "New Record Created successfully. Last id: ". $lastid;
}else{
die("Error ".$sql."<br>".$conn->error);
}
$conn->close();
As a beginner i don't where is the problem. I wrote the exact code like the tutorial but
Here is a link to the tutorial. http://www.w3schools.com/php/php_mysql_prepared_statements.asp
尝试下面的代码
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
$sql .= ",('Mary', 'Moe', 'mary@example.com')";
$sql .= ",('Julie', 'Dooley', 'julie@example.com')";
You have to add a ;
between two sql statement:
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";
或者,而不是多个查询,为什么不插入批次:
$sql = "
INSERT INTO MyGuests (firstname, lastname, email)
VALUES
('John', 'Doe', 'john@example.com'),
('Mary', 'Moe', 'mary@example.com'),
('Julie', 'Dooley', 'julie@example.com')
";
链接地址: http://www.djcxy.com/p/94514.html
上一篇: INSERT后的返回值
下一篇: INSERT INTO MySQL表错误