0

I intend to use an array to insert into my database, but only the first data have been inserted into the database and did not insert the other value, below is my code.

$orderID=mysqli_insert_id($con);
    $query2="INSERT INTO user_order(orderID, productName,productPrice, Quantity) VALUES (?,?,?,?)";
    
    $stmt=mysqli_prepare($con,$query2);
    
    mysqli_stmt_bind_param($stmt,"isii", $orderID, $productName, $productPrice, $Quantity);
    
    foreach($_SESSION['cart'] as  $key => $values)
    {
        
        $productName = $values['Item_Name'];
        $productPrice = $values['Price'];
        $Quantity = $values['Quantity'];
        mysqli_stmt_execute($stmt);
        
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0

You can try:

foreach ($records as $row) {
    $fieldVal1 = mysql_real_escape_string($records[$row][0]);
    $fieldVal2 = mysql_real_escape_string($records[$row][1]);
    $fieldVal3 = mysql_real_escape_string($records[$row][2]);
    $query ="INSERT INTO programming_lang (field1, field2, field3) VALUES ( '". $fieldVal1."','".$fieldVal2."','".$fieldVal3."' )";
    mysqli_query($conn, $query);
}
-1

You are binding the param before they assign.

Try this

foreach($_SESSION['cart'] as  $key => $values){
        $productName = $values['Item_Name'];
        $productPrice = $values['Price'];
        $Quantity = $values['Quantity'];
        
        $orderID = mysqli_insert_id($con);
        $query2 = "INSERT INTO user_order(orderID, productName,productPrice, Quantity) VALUES (?,?,?,?)";
    
        $stmt = mysqli_prepare($con,$query2);
    
        mysqli_stmt_bind_param($stmt,"isii", $orderID, $productName, $productPrice, $Quantity);

        mysqli_stmt_execute($stmt);
        
    }
S N Sharma
  • 1,436
  • 2
  • 7
  • 20