Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Sprintf在mysql中插入NULL_Php_Mysql_Printf - Fatal编程技术网

Php Sprintf在mysql中插入NULL

Php Sprintf在mysql中插入NULL,php,mysql,printf,Php,Mysql,Printf,早上好,我拼命想解决我在MySQL 5.7.19中插入空白日期值的问题,3天后我转到这里寻求帮助 DB被设置为允许NULL-默认NULL,前端字段有时被填充,通常不是空值 将弹出错误: 无法执行SQL语句:第1行“signedupdate”列的日期值“”不正确 插入 $lastInsertId = $this->GetConnection()->GetLastInsertId(); $sql = sprintf("INSERT INTO tbl_lead (client_id, si

早上好,我拼命想解决我在MySQL 5.7.19中插入空白日期值的问题,3天后我转到这里寻求帮助

DB被设置为允许NULL-默认NULL,前端字段有时被填充,通常不是空值

将弹出错误:

无法执行SQL语句:第1行“signedupdate”列的日期值“”不正确

插入

$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);

有人能看到我可能出错的地方吗?

在执行查询字符串之前,尝试回显该查询字符串,并在phymyadmin中复制/粘贴该回显查询,并检查查询中的错误

$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']);
echo $sql;

$this->GetConnection()->ExecSQL($sql);
在SQL(与PHP一样)中,
NULL
值与恰好包含字母N-U-L-L的常规文本变量之间存在很大差异。只要源变量是实际的
NULL
(而不是文本
'NULL
),任何像样的数据库库都会自动处理这一点您可以按预期使用库

您使用的是自定义数据库库,因此很难说是哪种情况。如果库不是很糟糕,它应该提供如下语法:

$sql = 'INSERT INTO tbl_lead (client_id, signedupdate, plan_type) VALUES (?, ?, ?)';
$this->GetConnection()->ExecSQL($sql, [
    $lastInsertId,
    $rowData['signedupdate'],
    $rowData['plan_type']
]);
当然,不一定是这种语法。请参阅“检查其源代码”的库文档

如果它是一个糟糕的库,它将只提供转义函数。如果这些函数自动添加引号,您可能会幸运地获得以下功能:

$sql = sprintf('INSERT INTO tbl_lead (client_id, signedupdate, plan_type) VALUES(%s, %s, %s)',
    $this->GetConnection()->EscapeValue($lastInsertId),
    $this->GetConnection()->EscapeValue($rowData['signedupdate']),
    $this->GetConnection()->EscapeValue($rowData['plan_type'])
);
$this->GetConnection()->ExecSQL($sql);
再说一遍,我只是编了语法

否则,你将不得不自己处理一切:

$sql = sprintf('INSERT INTO tbl_lead (client_id, signedupdate, plan_type) VALUES(%s, %s, %s)',
    $lastInsertId===null ? 'NULL' : "'" . $this->GetConnection()->EscapeValue($lastInsertId) . "'",
    $rowData['signedupdate']===null ? 'NULL' : "'" . $this->GetConnection()->EscapeValue($rowData['signedupdate']) . "'",
    rowData['plan_type']===null ? 'NULL' : "'" . $this->GetConnection()->EscapeValue($rowData['plan_type']) . "'"
);
$this->GetConnection()->ExecSQL($sql);

如果库甚至不提供转义函数,您应该真正停在这里,切换到例如PDO。根据我的经验,切换可能是个好主意,奇怪的是CasedLibraries()的质量往往令人怀疑。

我鼓励您使用准备好的语句来防止sql注入。
var\u dump($lastInsertId、$rowData['signedupdate',$rowData['plan\u type'])的输出是什么
?测试您正在传递的每个参数,%s将使用字符串类型,请尝试在sprintf之前添加测试,使用is_字符串验证它们是否为正确的类型。如果您不想为字段插入值,并且它有一个默认值(甚至为NULL),请在insert语句中跳过它。不要传递“”这是一个值(为空,但仍然是一个值),并将根据该字段的数据类型进行计算:如果是日期,则将失败我在phpmyadmin中运行此操作并收到以下错误。因此,似乎不接受“”,但接受NULL,0000-01-01也接受。
我确实需要它为NULL,以避免后期出现报告问题,但无法完全理解php为什么要作为字符串插入?
“插入tbl_lead(客户端id、签名更新、计划类型)值(20943、”;{“success”:false,“message”:“无法执行SQL语句:第1行的'signedupdate'列的日期值不正确”,“messageDisplayTime:0}”