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
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]')";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With