0

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!

  • 1
    add a prefix or suffix to your column – Aboodred1 May 13 '13 at 18:46
  • Did you mean your query worked in a normal INSERT INTO query ? I doubt. – AyeshK May 14 '13 at 7:05
1

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.
| 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 '13 at 21:10
1

Use '`parenthesis`' inside single qoute. It worked for us.

| improve this answer | |
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. – Bala Mar 5 '14 at 6:55
  • @Bala actually it is an answer. And as far as I can tell, it should work to solve problem from question's title (I admit I didn't read a whole question now). – Mołot Mar 5 '14 at 7:24
  • @Mołot While reading this in review queue, I don't understand easily and also in Meta Stack Overflow it will automatically convert to comment, I don't know why they not implemented here. meta.stackexchange.com/questions/211213/… – Bala Mar 5 '14 at 7:28
  • @Bala well, "skip" is always a valid option in review queue. Also, it's different when dumb script removes answer - scripts are quite expected to be dumb, so here are we now, human reviewers with the ability to comprehend, and skip if we are unsure. – Mołot Mar 5 '14 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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