0

I am using drupal 7.41, I have a database table and it contains an auto incremented column/field. I am trying to insert some data into that table by using db_insert but it keeps displaying syntax error. Am I missing something in this syntax?

db_insert('my_table')
-> fields(array(
    'id' => NULL, //my auto incremented field...
    'name' => $form_state['values']['name'],
    'link' => $form_state['values']['link],
    'group' => 2 /* I have already tried this field with and without integer data and string data. Eg '2' and 2*/
    )) -> execute();

My mysql table contains four columns: id (Auto incremented, int), name (varchar(50)), link(varchar(200)), group (int).

I have tried every syntatic way but no to avail. And lastly here is my error message when I execute the query above:

enter image description here

I think there is no access violation here, since I was able to insert some other rows into different tables. Only difference is that that table has no auto incremented integer field and other integer fields. Just two varchar fields.

Thank you.

2 Answers 2

2

The problem is you are using the keyword GROUP as a column name. Drupal's PDO code doesn't always escape column names, so you should avoid using MySQL keywords.

See the MySQL documentation.

Most of the reserved words in the table are forbidden by standard SQL as column or table names (for example, GROUP).

2
  • The PHP syntax is also invalid...
    – Clive
    Commented Nov 12, 2015 at 11:45
  • 1
    Oh yes. Probably just a transcription error though as he wouldn't be getting the PDO exception in that case. Commented Nov 12, 2015 at 11:49
0

You have used MySQL keyword "group" as a column name, so you should change MySQL keyword or use grave accent key with filed name.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.