duanbei2914 2017-06-15 10:41
浏览 38
已采纳

MySQLi bind_param UPDATE不使用空值

I have the following MySQL query that appears sound but wont update the table when one variable is empty

$stmtEmploymentDetails = getDB()->prepare(
    'UPDATE EmploymentDetails SET BranchOffice=?, EmploymentDate=?, EmploymentStatus=?, EmploymentType=?, OrdinaryHours=?, JobClassification=?, ManagmentGroupID=? WHERE EmployeeID=?'
);

and the binding param

$stmtEmploymentDetails->bind_param(
    'ssiiisss',
    $branchOffice,
    $employmentDate,
    $employmentStatus,
    $employmentType,
    $ordinaryHours,
    $jobClassification,
    $managmentGroupID,
    $employeeID
);

$stmtEmploymentDetails->execute;

I have also tried using COALESCE but the value (I'm assuming) is empty not null

$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=?'
);

This same approach works without any problems on a smaller query with

$stmtEmployeeProfile = getDB()->prepare(
    'UPDATE EmployeeProfiles SET AKAName=? WHERE EmployeeID=?'
);

and

$stmtEmployeeProfile->bind_param(
    'ss',
    $AKAName,
    $employeeID
);

$stmtEmployeeProfile->execute();

But in that query the values are always supplied. Anyone know where the problem is?

  • 写回答

1条回答 默认 最新

  • doujingqu3030 2017-06-15 10:51
    关注

    Have you put any error reporting around the query to see what is actually happening?

    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;
     }
    

    I hope that helps a little bit, when you have this error then work from that. Could be an issue of spelling or you haven't done the correct checks for values being empty. Way of doing this is a simple isset() statement such as.

    if (isset($stmtEmplotmentDetails)) {
    // do something 
    } else {
    $stmtEmplotmentDetails = 0; // or default value e.g. "N/A"
    }
    

    Also thinking on from have you checked the datatypes are correct to corrospond with the database, you have strings and integers in your bind parameter query. Do they match up with the datatype within your database?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 Qt 不小心删除了自带的类,该怎么办
    • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
    • ¥15 有没有哪位厉害的人可以用C#可视化呀
    • ¥15 可以帮我看看代码哪里错了吗
    • ¥15 设计一个成绩管理系统
    • ¥15 PCL注册的选点等函数如何取消注册
    • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
    • ¥15 Python爬虫程序
    • ¥15 crypto 这种的应该怎么找flag?
    • ¥15 代码已写好,求帮我指出错误,有偿!