我有以下MySQL查询显示声音,但当一个变量为空时不会更新表
$stmtEmploymentDetails = getDB()->prepare(
'UPDATE EmploymentDetails SET BranchOffice=?, EmploymentDate=?, EmploymentStatus=?, EmploymentType=?, OrdinaryHours=?, JobClassification=?, ManagmentGroupID=? WHERE EmployeeID=?'
);
和绑定参数
$stmtEmploymentDetails->bind_param(
'ssiiisss',
$branchOffice,
$employmentDate,
$employmentStatus,
$employmentType,
$ordinaryHours,
$jobClassification,
$managmentGroupID,
$employeeID
);
$stmtEmploymentDetails->execute;
我也试过使用COALESCE
,但是值(我假设)是空的而不是空
$stmtEmploymentDetails = getDB()->prepare(
'UPDATE EmploymentDetails SET BranchOffice=COALESCE(BranchOffice=?, BranchOffice), EmploymentDate=COALESCE(EmploymentDate=?, EmploymentDate), EmploymentStatus=COALESCE(EmploymentStatus=?, EmploymentStatus), EmploymentType=COALESCE(EmploymentType=?, EmploymentType), OrdinaryHours=COALESCE(OrdinaryHours=?, OrdinaryHours), JobClassification=COALESCE(JobClassification=?, JobClassification), ManagmentGroupID=COALESCE(ManagmentGroupID=?, ManagmentGroupID) WHERE EmployeeID=?'
);
使用
进行较小的查询时,同样的方法也没有任何问题$stmtEmployeeProfile = getDB()->prepare(
'UPDATE EmployeeProfiles SET AKAName=? WHERE EmployeeID=?'
);
和
$stmtEmployeeProfile->bind_param(
'ss',
$AKAName,
$employeeID
);
$stmtEmployeeProfile->execute();
但在该查询中,始终提供值。 谁知道问题出在哪里?
答案 0 :(得分:1)
您是否在查询周围放置了任何错误报告以查看实际发生的情况?
if ($stmtEmploymentDetails = getDB()->prepare(
'UPDATE EmploymentDetails
SET BranchOffice=?, EmploymentDate=?,
EmploymentStatus=?, EmploymentType=?,
OrdinaryHours=?,
JobClassification=?,
ManagmentGroupID=?
WHERE EmployeeID=?')) {
$stmtEmploymentDetails->execute();
} else {
echo "Failed prepare statement" . $this->conn->error . $this->conn->error;
}
我希望有一点帮助,当你有这个错误然后工作。可能是拼写问题,或者您没有对值为空的正确检查。这样做的方法是一个简单的isset()语句,例如。
if (isset($stmtEmplotmentDetails)) {
// do something
} else {
$stmtEmplotmentDetails = 0; // or default value e.g. "N/A"
}
另外考虑一下,如果你检查数据类型是否正确与数据库相互矛盾,你的绑定参数查询中有字符串和整数。它们是否与数据库中的数据类型匹配?