dongqin5604 2013-10-31 13:02
浏览 14
已采纳

如何根据以前的字段是否被占用来更新记录? [关闭]

Is there something wrong with my syntax its not executing in my php script? I am not updating all the records i am trying to update where invoice_no is equal to the '$id' from another form but only if pp1_dt, pp1_amt, pp1_ref is empty else move on to pp2_dt,pp2_amt,pp2_ref and so on to 5.

$i=1;

while($i <= 5) {
    $pp_sql =  "UPDATE Invoices SET pp'$i'_dt = '$pp1_dt', pp'$i'_amt = '$pp1_amt', pp'$i'_ref = '$pp1_ref' where invoice_no='$id' AND (coalesce(pp'$i'_dt, pp'$i'_amt, pp'$i'_ref) is null)"; 

    if($db->exec($pp_sql)) {
        $p_num = $i; 
    }
    else {
        $i++;  
    }
}
  • 写回答

2条回答 默认 最新

  • dongzan2740 2013-10-31 13:51
    关注

    you can't use your counter variable inside your string, you need to concatenate it

    $pp_sql =  "UPDATE Invoices SET pp" . $i . "_dt = '$pp1_dt', pp" . $i . "_amt = '$pp1_amt', pp" . $i . "_ref = '$pp1_ref' where invoice_no=" . $id . " AND (coalesce(pp" . $i . "_dt, pp" . $i . "_amt, pp" . $i . "_ref) is null)";
    

    the way you have it written now, it's trying to find columns in your table called pp$i rather than pp1, pp2, etc, which don't exist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?