Reputation: 43
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', '[email protected]')INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Mary', 'Moe', '[email protected]')INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Julie', 'Dooley', '[email protected]')
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', '[email protected]')";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]')";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')";
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
Upvotes: 4
Views: 4133
Reputation: 41885
Or instead of multiple queries, why not insert batches:
$sql = "
INSERT INTO MyGuests (firstname, lastname, email)
VALUES
('John', 'Doe', '[email protected]'),
('Mary', 'Moe', '[email protected]'),
('Julie', 'Dooley', '[email protected]')
";
Upvotes: 2
Reputation: 3766
Put semicolon(;) after each insert statement because you are executing more than one statement at a time
INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]');
INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Mary', 'Moe', '[email protected]');
INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Julie', 'Dooley', '[email protected]');
Hope this works for you.
Upvotes: 0
Reputation: 928
Try Following code
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";
$sql .= ",('Mary', 'Moe', '[email protected]')";
$sql .= ",('Julie', 'Dooley', '[email protected]')";
Upvotes: 3
Reputation: 27072
While you run more queries, you need to end each one by semicolon.
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]');";
^
Upvotes: 1
Reputation: 69440
You have to add a ;
between two sql statement:
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')";
Upvotes: 3