php PDO insert batch multiple rows with placeholders

前端 未结 5 1435
失恋的感觉
失恋的感觉 2020-12-01 10:53

I am looking to do multiple inserts using PHP PDO.

The closest answer I have found is this one

how-to-insert-an-array-into-a-single-mysql-prepared-st

5条回答
  •  一整个雨季
    2020-12-01 11:18

    Your code was actually ok, but had a problem in $stmt->bindParam(":$column", value); It should be $stmt->bindValue(":{$column}", $value); and it will work perfectly. This will assist others in future.

    Full code:

    foreach($params as $row)
    { 
        // now loop through each inner array to match bound values
        foreach($row as $column => $value)
        { 
            $stmt->bindValue(":{$column}", $value); //EDIT
        }
        // Execute statement to add to transaction
        $stmt->execute();
    } 
    

提交回复
热议问题