INSERT INTO MySQL表错误
使用w3schools的MYSQL学习PHP。 一切都很好,但当我试图在表中输入多个值,并显示下面的错误。
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
这是我的代码示例
$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();
作为初学者,我不知道问题出在哪里。 我写了像教程那样的确切代码
这里是教程的链接。 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')";
你必须添加一个;
两个sql语句之间:
$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/94513.html
上一篇: Error INSERT INTO MySQL Table
下一篇: Inserting multiple rows in one table with just one insert commnad