简体   繁体   中英

SQL Syntax Error - Insert Operation (PHP)

I've used this code numerous times without issue, and cannot determine why I have a syntax error. I am currently inserting "dummy" data, to confirm that my variables were not the issue.

$connection = mysqli_connect($hostname,$username,$password,$database);
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
  echo "connection success <br>"; 
}

$query = "INSERT INTO Radio (State, City, Call, Dial, Lat, Long, Lat2, Long2) VALUES ('abc', 'abc', 'abc', '2', 'abc', 'abc', 'abc', 'abc')";
$result = mysqli_query($connection, $query);
printf("Errormessage: %s\n", mysqli_error($connection));
mysqli_close($connection);

I am getting the following output:

connection success 
Errormessage: 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 'Call, Dial, Lat, Long, Lat2, Long2) VALUES ('abc', 'abc', 'abc', '2', 'abc', 'ab' at line 1

I know my connection is working, and that I have a syntax error. That being said I cannot spot an issue with my syntax. I have 8 fields, and am providing 8 values. The field names match my database definition precisely:

数据库定义

Any ideas? Nothing is being inserted, at all. Thanks.

Turns out "CALL" is a reserved word, so I can't use it as a database column. Bummer. Problem Solved. Same with "LONG". Should have realized this sooner. Maybe this will help someone else.

CALL是保留字,请使用其他名称

As pointed out Call is a reserved word. You can still use this a column name if really need, you would just need to escape it each time you use it in a query. Like so

"INSERT INTO Radio (State, City, `Call`, Dial, Lat, Long, Lat2, Long2) VALUES ('abc', 'abc', 'abc', '2', 'abc', 'abc', 'abc', 'abc')"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM