如何插入null date mysql real scape string
if (empty($DATE_PURCHASED)) {
$DATE_PURCHASED = NULL;
}
//sql query
$queryString = sprintf("INSERT INTO IT_ASSET_RADIO (BRAND_NAME, SERIAL_NO, DATE_PURCHASED, LICENSE_NO)
values ('%s','%s', '%s', '%s')",
mysql_real_escape_string($BRAND_NAME),
mysql_real_escape_string($SERIAL_NO),
mysql_real_escape_string($DATE_PURCHASED),
mysql_real_escape_string($LICENSE_NO));
$query = mysql_query($queryString ) or die("Insert failed: " . mysql_error());
如果是值,我在DATE_PURCHASED中收到此查询无效日期的错误 为空
答案 0 :(得分:1)
$date=mysql_real_escape_string($DATE_PURCHASED);
if(trim($date)==''){ $date=NULL;}
在查询中使用$ date。
还
在表格中设置 DATE_PURCHASED 列默认为空
答案 1 :(得分:0)
尝试以' YYYY-MM-DD HH:MM:SS'等格式传递日期字符串,如下所示:
..........mysql_real_escape_string(date('Y-m-d H:i:s', strtotime($DATE_PURCHASED))).....
答案 2 :(得分:0)
if (empty($DATE_PURCHASED)) {
$DATE_PURCHASED = NULL;
} else {
$DATE_PURCHASED = $data->DATE_PURCHASED;
}
// Surround it in quotes if it isn't NULL.
if ($DATE_PURCHASED === NULL) {
// Make a string NULL with no extra quotes
$DATE_PURCHASED = 'NULL';
}
// For non-null values, surround the existing value in quotes...
else $DATE_PURCHASED = "'$DATE_PURCHASED'";
$queryString = sprintf("INSERT INTO IT_ASSET_RADIO (BRAND_NAME, SERIAL_NO, DATE_PURCHASED, LICENSE_NO)
values ('%s','%s', $DATE_PURCHASED , '%s')",
mysql_real_escape_string($BRAND_NAME),
mysql_real_escape_string($SERIAL_NO),
mysql_real_escape_string($LICENSE_NO));
$query = mysql_query($queryString ) or die("Insert failed: " . mysql_error());