1

This is my code but I am getting an SQL Syntax error;

$insert = "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area1."')";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area2."')";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area3."')";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area4."')";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area5."')";

What is the correct way to carry out this query?

3 Answers 3

3

When inserting multiple tuples, this is the notation used.

$insert = "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area1."')";
$insert .= ", (NULL, '".$area2."')";
$insert .= ", (NULL, '".$area3."')";
$insert .= ", (NULL, '".$area4."')";
$insert .= ", (NULL, '".$area5."')";
$insert .= ";";

Providing multiple INSERT clauses suggests separate queries; which could be acceptable depending on MySQL library, but each would need to be ; terminated.

That said, you really should be using parameterized queries. When prepared, their speed is usually comparable to this (outside of some larger bulk inserts).

Sign up to request clarification or add additional context in comments.

Comments

0

You need to specify the column names only once and then values separated by commas. eg

INSERT INTO students (Name, Age) VALUES ('John',10), ('Bob',10), ('Jeremy',10);

So just replace "INSERT IGNORE INTO locations (location_id, location_name) VALUES " in all statements except the first one with a comma "," and also insert semi colon at the end.

Comments

0

USE This SQL Syntax (use ;):

$insert = "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '$area1');";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '$area2');";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '$area3');";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '$area4');";

Or

$insert = "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '$area1')";
$insert .= ", (NULL, '$area2')";
$insert .= ", (NULL, '$area3')";
$insert .= ", (NULL, '$area4');";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.