i am getting this error when i tried to insert the data from the custom form field i create:

user warning: 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 'uid`, `status`, `created`, `changed`, `comment`, `promote`, `moderate`, `sticky`' at line 1 query: INSERT INTO node (`nid`, `vid`, `type`, `language`, `title', `uid`, `status`, `created`, `changed`, `comment`, `promote`, `moderate`, `sticky`, `tnid`, `translate`) VALUES ( '62', '62', 'suggesttag', '', 'hello', 1, '', 1245402293, 1245402293, '', '', '', '', '', '') in C:\wamp\www\teagal\sites\all\modules\suggestform\suggestform.module on line 93.

I wrote a insert query as

db_query("INSERT INTO {node} (`nid`, `vid`, `type`, `language`, `title', `uid`, `status`, `created`, `changed`, `comment`, `promote`, `moderate`, `sticky`, `tnid`, `translate`) VALUES ( '$nid', '$vid', '$type', '', '$title', $uid, '', $time, $time, '', '', '', '', '', '')");

as in the query i kept some of the field empty cos i want it to be empty only..

the above query i wrote is inside the custom module i created ...

please do have help with the question.. i m confused what should i do..

Comments

udrulz’s picture

You have totally messed up ur double quotes, single quotes and ` ...

look at this part .`language`, `title', `uid` ...

Firstly I dont understand why are you using ` in the first place ..

2nd, you're supposed to use placeholders while using db_query() like this %d,%s etc ..

3RD: ALL FIELDS IN THE NODE TABLE ARE NOT NULL. so no empty values. Hence, insert ONLY till "changed".

Please read Pro Drupal Development 2nd edition.

vijayg’s picture

thanks for the suggestion.. i wil change it and c wat wil be the output.. and get back to u.

god bless u for the effort u made

vijayg’s picture

i have,ade the changes but still the same error is coming.. check it out the code:

db_query('INSERT INTO {node} (nid, vid, type, language, title, uid, status, created, changed, comment, promote, moderate, sticky, tnid, translate) VALUES ( %d, %d, %s, %s, %s, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)' , $nid, $vid, $type, ' ', $title, $uid, ' ', $time, $time, ' ', ' ', ' ', ' ', ' ', ' ');

this is an error:
user warning: 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 ' victor, 1, 0, 1245408668, 1245408668, 0, 0, 0, 0, 0, 0)' at line 1 query: INSERT INTO node (nid, vid, type, language, title, uid, status, created, changed, comment, promote, moderate, sticky, tnid, translate) VALUES ( 62, 62, suggesttag, , victor, 1, 0, 1245408668, 1245408668, 0, 0, 0, 0, 0, 0)

please look forward for ur suggestion

udrulz’s picture

This is how perform an insert query using drupal.:

db_query("INSERT INTO {annotations} (nid, uid, note, created) VALUES (%d, %d, '%s', %d)", $nid, $user->uid, $note, time());

see the single quotes for %s? ..
Also, dont include the columns which dont require an insert. The default value will get inserted..

db_query("INSERT INTO {node} (nid,vid,type,title,uid,created,changed) VALUES (%d,%d,'%s','%s',%d,%d) ,$nid, $vid, $type, $title, $uid, $time);

vijayg’s picture

hey udrulz
thanks for ur prety effort.. really encouragig for me... as u saved me from boss screwing .....

really appreciated

danielb’s picture

I hate to point this out, but if you're working with existing node fields you don't need to do database queries, just build a $node object and use node_save().