jack
jack

Reputation: 11

PDO insert array is only showing one entry

I am try to insert an array into a table with PDO and it is only inserting the first (or last, depending how you look at it) item in the array. Here is my code:

$pdo = new PDO("mysql:charset=utf8mb4;host=$servername;dbname=$dbname", $username, $password);


$sql = "INSERT INTO orderitems 
                (pid, 
                 name, 
                 style, 
                 weight,
                 price,
                 quantity) 
    VALUES      (:pid, 
                 :name, 
                 :style, 
                 :weight, 
                 :price,
                 :quantity) ";

$stmt= $pdo->prepare($sql);


foreach ($order as $order) {

     $stmt->execute(array (
        'pid'       => $order['id'],
        'name'      => $order['name'],
        'style'     => $order['style'],
        'weight'    => $order['weight'],
        'price'     => $order['price'],
        'quantity'  => $order['quantity']
    ));
     
}

Upvotes: 0

Views: 40

Answers (1)

Masoud Shariati
Masoud Shariati

Reputation: 37

The problem may be here:

foreach ($order as $order) {

Probably, the first $order variable, as it's naming suggests, is not an array.

Upvotes: 1

Related Questions