简体   繁体   中英

PHP SQL syntax error for insert into

I have a bunch of php files corresponding to an application I am writing, using MySQL for my database structure. I know this questions has been asked before but I've been through most of the posts about it and can't find something that will help...

In my PHP file I have a SQL query

$group_sql = "INSERT INTO group (name, description, ownerEmail) VALUES ('$groupName', '$descrip', '$owner')";

that corresponds to a group table with three attributes: name, description, and owner email. $groupName, $descrip, $owner are three variables I have defined. I'm getting this syntax error when I try to run the query:

Error: INSERT INTO group(name, description, ownerEmail) VALUES(hi, hi, test@example.com) 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 'group(name, description, ownerEmail) VALUES(hi, hi, test@example.com)' at line 1

Can someone please help me see what I'm doing wrong?

GROUP is a MySQL reserved keyword . If you name a table that, then you must wrap it in ticks:

$group_sql = "INSERT INTO `group` (name, description, ownerEmail)  
              VALUES ('$groupName', '$descrip', '$owner')";

Notice where SQL starts with the error and points to it?

>for the right syntax to use near 'group  
>                                 ^

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