1
votes

I try a simple insert query in Drupal 7, but it always return with error. I tried out db_insert, drupal_write_record, and db_query also, but every functions return various errors.

I have a tabledrag_menu table:

enter image description here

I have an $adat array:

Array
(
    [name] => Overview
    [weight] => 0
    [group] => left
    [checked] => 0
    [tid] => 96
    [nid] => 20
)

With var_dump it looks like this:

array(6) { ["name"]=>   string(8) "Overview"   ["weight"]=>   int(0)   ["group"]=>   string(4) "left"   ["checked"]=>   int(0)   ["tid"]=>   int(96)   ["nid"]=>   int(20) } 

My code, which gives the error:

$id = db_insert('tabledrag_menu')->fields($adat)->execute();

When i use the db_insert, it return with this error message:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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, checked, tid, nid) VALUES ('Overview', '0', 'left', '0', '96', '20')' at line 1: INSERT INTO {tabledrag_menu} (name, weight, group, checked, tid, nid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5); Array ( [:db_insert_placeholder_0] => Overview [:db_insert_placeholder_1] => 0 [:db_insert_placeholder_2] => left [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => 96 [:db_insert_placeholder_5] => 20 )

I think, this part causes the error, but i don't know how to fix it:

VALUES ('Overview', '0', 'left', '0', '96', '20')

2

2 Answers

6
votes

Group is MySql reserved word. Try using some other name for that field.

1
votes

Try to remove the single quote from the values that go inside integer columns.

E.g. change this

VALUES ('Overview', '0', 'left', '0', '96', '20')

to be something like:

VALUES ('Overview', 0, 'left', 0, 96, 20)