duankao4489 2019-06-11 09:12
浏览 281

如果新值在数组中,如何批量更新数据库? PHP

I am trying to do an UPDATE statement for multiple records for the edit function. The page has multiple rows that requires to be updated. I am not sure how to construct the statement if the parameter passed is an array.

arr_items[[arr_items2]] </br>
arr_items[[value1, value2, value3, value4], [value1, value2, value3, value4]]

I tried different bind_param also but it won't work.

edit-view.php

$dao5->modify($arr_items)

DAO.php

public function modify($arr_items) {
        $sql = 'update table set 
                column3=:???(supposedly value3) 
                column4=:???(supposedly value4) 
                where column1=:???(supposedly value1) and column2=:???(supposedly value2);      

        $values = $arr_items;

        for ($i=0; $i < count($values); $i++){
            $stmt = $conn->prepare($sql);
        }

        $connMgr = new ConnectionManager();           
        $conn = $connMgr->getConnection();

       call_user_func_array(array($stmt, 'bind_param'), $values);

        $stmt->execute();
    }

Error Message: ``` Uncaught Error: Call to a member function prepare() on null in C:

  • 写回答

1条回答 默认 最新

  • dongxue9997 2019-06-13 01:38
    关注

    after experimenting the sql should be as per below and loop inside the statement to modify each record.

           $sql = 'update table set column3=:value3, column4=:value4 where column1=:value1 and column2=:value2 ';      
    
            $values = $arr_items;
    
            for ($i=0; $i<count($values); $i++){
                $query_p = array(':value3'=> $arr_items[$i][2], ':value4' => $arr_items[$i][3], ':value1' => $arr_items[$i][0], ':value2' => $arr_items[$i][1]);
    
                $stmt->execute($query_p);
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line