Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error INSERT INTO MySQL Table

Tags:

php

mysql

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

like image 798
Muminul Haque Avatar asked Jan 08 '23 09:01

Muminul Haque


1 Answers

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]')";
like image 115
Paresh Thummar Avatar answered Jan 15 '23 14:01

Paresh Thummar