PDO 传递布尔值而不是绑定值
PDO passing bool values rather than the bind values
我正在尝试将条目添加到 table 中(如果它不存在)或更新它(如果它存在)。
$sql = "INSERT INTO company
(userid, companyName, registeredAddress, registeredPostcode)
VALUES ($userid, companyName = :companyName, registeredAddress = :registeredAddress, registeredPostcode = :registeredPostcode)
ON DUPLICATE KEY UPDATE
companyName = :companyName,
registeredAddress = :registeredAddress,
registeredPostcode = :registeredPostcode;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute(['companyName' => $companyname,
'registeredAddress' => $registeredaddress,
'registeredPostcode' => $registeredpostcode]);
如果用户 ID 已经存在,则更新完美。
如果没有用户 ID,它会创建一个包含用户 ID 的行,然后在其他字段中放置 0 或 1。如果有值则为 0,如果未传递值则为 1。
知道我哪里出错了吗?
语法看起来不对。在 insert
的 values
子句中,您只需要传递值,而不是 =
表达式:
$sql = "INSERT INTO company (userid, companyName, registeredAddress, registeredPostcode) VALUES ($userid, :companyName, :registeredAddress, :registeredPostcode) ON DUPLICATE KEY UPDATE companyName = :companyName,registeredAddress = :registeredAddress, registeredPostcode = :registeredPostcode;";
旁注:
$userid
也应该作为绑定变量传递(它不在原始代码中)
我正在尝试将条目添加到 table 中(如果它不存在)或更新它(如果它存在)。
$sql = "INSERT INTO company
(userid, companyName, registeredAddress, registeredPostcode)
VALUES ($userid, companyName = :companyName, registeredAddress = :registeredAddress, registeredPostcode = :registeredPostcode)
ON DUPLICATE KEY UPDATE
companyName = :companyName,
registeredAddress = :registeredAddress,
registeredPostcode = :registeredPostcode;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute(['companyName' => $companyname,
'registeredAddress' => $registeredaddress,
'registeredPostcode' => $registeredpostcode]);
如果用户 ID 已经存在,则更新完美。
如果没有用户 ID,它会创建一个包含用户 ID 的行,然后在其他字段中放置 0 或 1。如果有值则为 0,如果未传递值则为 1。
知道我哪里出错了吗?
语法看起来不对。在 insert
的 values
子句中,您只需要传递值,而不是 =
表达式:
$sql = "INSERT INTO company (userid, companyName, registeredAddress, registeredPostcode) VALUES ($userid, :companyName, :registeredAddress, :registeredPostcode) ON DUPLICATE KEY UPDATE companyName = :companyName,registeredAddress = :registeredAddress, registeredPostcode = :registeredPostcode;";
旁注:
$userid
也应该作为绑定变量传递(它不在原始代码中)