I have a table called foo_bar whose fields are as follows
id int(10) unsigned NOT NULL AUTO_INCREMENT
name varchar(255) DEFAULT NULL
amount float DEFAULT NULL
I want to enter the value to this table like this
$name_value = 'Dollar';
$amount = NULL;
db_query("INSERT INTO {foo_bar} (name, amount) VALUES ('%s', %d)", $name_value, $amount_value);
But now when I see the values into the table it showed me
id | name | amount |
1 | Dollar | 0 |
I tried db_write_record as well like this...
$obj_foo_bar = new stdClass();
$obj_foo_bar->name = 'Dollar'
$obj_foo_bar->amount = ''
But I am getting the same result.
EDIT: 17 Sep 2012
Don't use drupal_write_record for insert/update queries where you want to place the default value to NULL
INSTEAD: Use content_write_record which is used by CCK module to update the database tables were you want to place default value as NULL.
This function says Directly copied from core's drupal_write_record, which can't update a column to NULL. See http://drupal.org/node/227677 and http://drupal.org/node/226264 for more details about that problem.