By Vic96 on
I have this in my code:
db_query("INSERT INTO {fruittable} (nid, vid, fruitid, fruitname) VALUES (%d, %d, %d, %s)",
$node->nid, $node->vid, $fruitid, 'Apple');
and I'm getting this error:
user warning: Unknown column 'Apple' in 'field list' query: INSERT INTO fruittable (nid, vid, fruitid, fruitname) VALUES (184, 184, 6, Apple) in C:\wamp\www\food\sites\all\modules\custom\fruit\fruit.module on line 397.
The table gets created correctly with varchar (15) for the fruitname field.
What could be wrong? Thanks.
Comments
Missing quotes
Your query is missing quotes for datatypes :
db_query("INSERT INTO {fruittable} (nid, vid, fruitid, fruitname) VALUES ('%d',' %d', '%d', '%s')",
$node->nid, $node->vid, $fruitid, 'Apple');
Almost
Only %s should be quoted.
%d
%d can be used with or without quotes.
Thanks! It works now!
Thanks! It works now!
"It works" is not equivalent
"It works" is not equivalent with "It is right".
But then again, I sometimes forget this is Drupal.org.