【发布时间】:2018-05-15 09:00:32
【问题描述】:
早上好,拼命尝试解决我在 MySQL 5.7.19 中插入空白日期值时遇到的问题,3 天后转向这里寻求帮助。
数据库设置为允许 NULL - 默认 NULL 有时会填充前端字段,通常不是这样的空值。
错误弹出:
无法执行 SQL 语句:日期值不正确:'' for column 'signedupdate' at row 1
插入
$lastInsertId = $this->GetConnection()->GetLastInsertId();
$sql = sprintf("INSERT INTO tbl_lead (client_id, signedupdate, plan_type) VALUES(%d, '%s', '%s');", $lastInsertId, $rowData['signedupdate'], $rowData['plan_type']);
$this->GetConnection()->ExecSQL($sql);
更新
$sql = sprintf("UPDATE tbl_lead SET signedupdate = '%s', plan_type = '%s'WHERE client_id = %d;", $rowData['signedupdate'], $rowData['plan_type']);
$this->GetConnection()->ExecSQL($sql);
谁能看出我哪里出错了?
【问题讨论】:
-
我鼓励你使用准备好的语句来防止sql注入。
-
var_dump($lastInsertId, $rowData['signedupdate'], $rowData['plan_type'])的输出是什么? -
测试您传递的每个参数,%s 将需要一个字符串类型,尝试使用 is_string 在 sprintf 之前添加测试以验证它们的类型是否正确。
-
如果你不想为一个字段插入一个值,并且它有一个默认值(甚至是NULL),只需在插入语句中跳过它。不要传递作为值(空白但仍然是值)的 '' 并将根据该字段的数据类型进行评估:如果它是一个日期,它将失败