我尝试使用PDO清除日期时间字段的值时出错。问题是,当prepare语句引用字符串时传递NULL值,然后SQL返回错误
Example:
//Test Values
$_POST['FromDate'] = 'NULL';
$_POST['ToDate'] = '20140905';
//Update statement
$SQLQuery = "UPDATE A
SET
FromDate = :FromDate
,ToDate = :ToDate
FROM dbo.TEST AS A
WHERE
ID = 1 ";
//User Params
$UserParams = array(':FromDate' => $_POST['FromDate']
,':ToDate' => $_POST['ToDate']
);
//Run the query
$s = $handler->prepare($SQLQuery);
$s->execute(array($UserParams));
SQL查询然后返回错误' NULL'是一个字符串,FromDate是DATETIME。它必须是NULL而不是' NULL'
UPDATE A
SET
FromDate = 'NULL'
etc...
答案 0 :(得分:3)
如果您想手动将其设置为NULL
,请不要使用post变量。
只需将其硬编码为PHP null对象(当然不是字符串'NULL'
)
$stmt = $handler->prepare($SQLQuery);
$success = $stmt->execute(array(':FromDate' => NULL,
':ToDate' => $_POST['ToDate']
));
if($success) {
echo 'updated!';
} else {
echo 'failed';
}