使用PHP和PDO进行foreach循环

时间:2016-12-08 06:27:31

标签: php pdo

这个mysql更新:

 function updateOrder($id_array){
    $count = 1;
    foreach ($id_array as $id){
      $update = mysqli_query($this->connect,"UPDATE `table_name` SET `order_id` = $count WHERE id = $id");
      $count ++;  
    }
    return true;
  }

我将改为PDO,这是我的写代码,但可以工作  但这没有反映在db

function update($id_array,$_table){

        $query = "UPDATE `table_name` SET `order_id` = ? WHERE id = ?";

        $stmt = $this->conn->prepare($query);  

        $count = 1;
         foreach ($id_array as $id) { 
    $stmt->execute(array(':$count' =>  $count, ':id' => $id));
      $count ++;
        }
          return true;
      }
呼叫功能:

$idArray    = explode(",",$_POST['ids']);<br>
$db->update($idArray);  

1 个答案:

答案 0 :(得分:4)

使用:

$stmt->execute(array(':count' =>  $count, ':id' => $id));

而不是

$stmt->execute(array(':$count' =>  $count, ':id' => $id));

:$ count :计数