0

I have an order page called 08012021.php, where you can insert x amount of gloves in different kind of sizes like small, medium, large and xlarge - when the user has entered the number of gloves they want in the different kinds of sizes, they will be directed to a different page called cart.php where they can review their order.

Here, they can see the order date by the product, different kinds of sizes they have chosen and the price of each kind of glove, and the total amount of gloves and total amount price. Working fine.

If the user agrees to this, I want to insert it into my database foodorder->orders, but I have no idea where to put the code inside my cart.php, so that when the press the button, it will insert into my database with the information.

The name of the database is foodorder, the table is called orders.

I have this code:

$gtotal = 0;
foreach($_SESSION["cart"] as $keys => $values)
{
  $F_ID = $values["food_id"];
  $foodname = $values["food_name"];
  $price =  $values["food_price"];
  $small = $values["small"];
  $total = ($values["small"] * $values["food_price"]);
  $R_ID = $values["R_ID"];
  $username = $_SESSION["login_user2"];
  $order_date = date('Y-m-d');

  $gtotal = $gtotal + $total;

  $query = "INSERT INTO orders (F_ID, foodname, price, small, R_ID, username, order_date) 
            VALUES ('" . $F_ID . "','" . $foodname . "','" . $price . "','" . $small . "','" . $R_ID . "','" . $username . "','" . $order_date . "');";

  $success = $conn->query($query); 
}

I don't know how to place the other code in here.

F. Müller
  • 3,969
  • 8
  • 38
  • 49
  • 2
    Please note that the way you're building your query is unsafe. You're open to [SQL injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). You should use [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – El_Vanja Nov 22 '20 at 14:31
  • 1
    If you want this to execute when the user submits a form, then just place this code inside and `if` that checks if it was submitted. Judging by the text you wrote, you should know how to handle form submissions. – El_Vanja Nov 22 '20 at 14:33
  • First, you should remove the `;` at the end of the query, that's needed only in CLI, not in PHP. Second, learn to use https://www.php.net/manual/en/mysqli.error.php , so thay you KNOW why your query has not succeeded – Ron Nov 23 '20 at 01:08

0 Answers0