Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQLi query in while loop not adding to database
    text
    copied!<p>I have got the following code to add a user's orders to a database. The orders are stored as an array ($row), which are then set to single variables in the while loop, which are then set as the query. No error message is displayed, but the records are not added to the MySQL database. I have put the area I think is the problem in bold. I have looked at several other help threads and tried them, but they won't work for this. </p> <p>I'm fairly new to PHP, so please can you try to keep answers in plain English where possible. Thanks if anyone can give me a hand with this.</p> <p></p> <pre><code>&lt;?php session_start(); ?&gt; &lt;?php //db connect require('connect_db.php'); //define variables $id=$_SESSION['user_id']; $fn=$_SESSION['first_name']; $ln=$_SESSION['last_name']; $bank=$_POST['bank']; $ano=$_POST['account']; $sort=$_POST['sort_code']; //get order details **$qa="SELECT total, product, quantity, FROM orders WHERE user_id='$id'"; $ra=mysqli_query($dbc, $qa); while($row=mysqli_fetch_array($r, MYSQLI_ASSOC)) { //insert to table $product=$row['product']; $quantity=$row['quantity']; $total=$row['total']; $qb="INSERT INTO order_contents(user_id, first_name, last_name, product, quantity, price, bank, account_no, sort_code) VALUES '$id', '$fn', '$ln', '$product', '$quantity', '$total', SHA1('$bank'), SHA1('$ano'), SHA1('$sort'))"; mysqli_query($dbc, $qb) or die(mysqli_error($dbc)); }** //clear db mysqli_query($dbc, "DELETE FROM orders WHERE user_id='$user_id'"); //mysql close mysqli_close($dbc); //redirect to thank you page header("Location:http://emsworthsailspares.net23.net/thanks.php"); ?&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta content="text/html; charset=utf-8" http-equiv="Content-Type" /&gt; &lt;title&gt;Untitled 1&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload