开发者

datetime insertion problem in drupal

$date_time = date("y-m-d h:i:s", time());

db_query("INSERT INTO {category}(category_name, sort_order, date, category_image)
VALUES ('%s', '%s', '%s', '%s')", $form['name']['#post'], $form['name']['#post'], $date_time, $form['name']['#post']);

here current date in not inserted... what should i use instead of %s ? or ho开发者_如何转开发w can i solve this problem. Want a easy reply please.

Thanks.


Use integer type for the datetime field. And try this :

$current_time = time();

db_query("INSERT INTO {category}(category_name, sort_order, date, category_image)
VALUES ('%s', '%s', %d, '%s')", $form['name']['#post'], $form['name']['#post'], $current_time, $form['name']['#post']);


try $date_time = date("Y-m-d H:i:s", time());

the h is 01-12 instead of H - 00 to 23

and back-tick the reserved keywords like

(category_name, sort_order, `date`, category_image)

PS: please don't use reserved keywords as column, is so troublesome


What type is the data for the 'date' column in your table? %s expects and only inserts string values - the php date function returns a string so you probably have a data type mismatch there. try adding this line:

$date_time=strtotime($date_time);

and then inserting it to your database. This should only work if the data type of the column is date or datetime.


Not a direct answer to your question but just some advice. In a Drupal module you should never use "INSERT" when writing to the database, instead use drupal_write_record ( http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_write_record/6).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜