Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

It appears that db_insert() (Drupal 7) fails when I try to specify a column name which is one of the reserved keywords (in this case, insert). I tried finding a way to use the back tick syntax which works with a direct SQL command, insert, but it still does not seem to work.

Googling so far has only confirmed this problem and returned suggestions to change the column name.

Is this the only solution, or is some other way to get db_insert to work (there's a good reason why the column is called insert in our application)

thanks!

share|improve this question
1  
add a prefix or suffix to your column – Aboodred1 May 13 at 18:46
Did you mean your query worked in a normal INSERT INTO query ? I doubt. – Ayesh K May 14 at 7:05

1 Answer

You can try to pass column name in `parenthesis` to db_insert. Just be sure you escaped them if needed, as they have their own meaning in php itself. If this does not work, well, you can:

  • fill a bug against core, or post in existing one.
  • use good old db_query
  • Or use thesaurus to get rid of reserved words from your app. There is always a "good reason" to use them, of course there is for any common word, but word-oriented languages simply have to reserve some words.
share|improve this answer
Thanks, figured out how to make it work. I have to use db_insert() instead of db_query() in order to get the last inserted id; nor can I get rid of the Insert keyword; So the syntax is: db_insert('tablename') ->fields(array( 'insert' => ...))->execute(); – user14760 May 14 at 21:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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