Unable to insert into tables

I am trying to create a small shopping cart and I am getting this error

Notice: Undefined index: quantity in C:\wamp\www\bakery\BakeryNewOrderConfirm.php on line 31

I am using the following php files and an sql file

bakerynew.php

Bakery

RVCC Bakery

<?php @ $db = new mysqli('localhost', 'root', '', 'bakery'); if (mysqli_connect_errno()) { echo 'Error: Could not connect to database. Please try again later.'; exit; } $query = "select * from product"; $result = $db->query($query); $num_results = $result->num_rows; for ($i=0; $i <$num_results; $i++) { $row = $result->fetch_assoc(); echo "
ProductId Product Name Unit Price Quantity
"; echo ""; echo ""; echo ""; echo ""; $quant = "quantity".$i; echo ""; echo ""; echo ""; echo "
".($i+1)."".$row['ProductName']."".$row['UnitPrice']."" .$quant. "
"; } if ($result != null) { $result->free(); $db->close(); } ?>

bakeryneworderdetails.php

Bakery Order Details

Bakery Order Details

RVCC Bakery

ProductName Unit Price Quantity Total Price
<?php //create short Variable Names $orderNow = $_POST['ordernow'];
@ $db = new mysqli('localhost', 'root', '', 'bakery');

if (mysqli_connect_errno()) {
  echo 'Error: Could not connect to database.  Please try again later.';
  exit;
}

$query = "select * from product";

$result = $db->query($query);
$num_results = $result->num_rows;

$totalPrice = 0;
// Note that $snacks will be an array.
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo “

”;
echo “”;
echo “";
echo “";
$quant = $_POST[‘quantity’.$i];
echo “";
$price = $row[‘UnitPrice’] * $quant;
$totalPrice += $price;
echo “";
echo “”;
echo "
”.$row[‘ProductName’]."”.$row[‘UnitPrice’]."”.$quant."”.$price."

";
}

echo "Total Price for this order: ".$totalPrice;
if ($result != null)
$result->free();
$db->close();
?>

If you wish to place this order, please enter your information to confirm the order

First Name:
Last Name:
Login Name:

bakeryneworderconfirm.php

Bakery Order Confirmation

Bakery Order Confirmation

Thank You for your order. Your order is successfully processed. Order Confirmation number is

<?php

@ $db = new mysqli(‘localhost’, ‘root’, ‘’, ‘bakery’);

if (mysqli_connect_errno()) {
echo ‘Error: Could not connect to database. Please try again later.’;
exit;
}

// Insert into Customer table first
$query =“insert into customer values (’”.$_POST[‘first’]."’, ‘".$_POST[‘last’]."’, ‘".$_POST[‘login’]."’);";
//echo $query;
$result = $db->query($query);
if ($result == null)
echo “Insert into customer Failed”;

// Insert into order_items table
$query = “select * from product”;
$result = $db->query($query);
$num_results = $result->num_rows;

$quant = $_POST[‘quantity’];
echo $quant[0];
echo $quant[1];
$totalPrice = 0;
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
$unitPrice = $row[‘UnitPrice’];
$quant = $_POST[‘quantity’.$i];
$price = $unitPrice * $quant;
$totalPrice += $price;
$orderId = 0;
$query =“insert into order_items values (’”.$row[‘ProductName’]."’, “.$orderId.”, “.$price.”, “.$quant.”)";
echo $query;
}

// Finally Insert into Orders table
$query =“insert into orders values (’”.$_POST[‘last’]."’, “.$orderId.”, “.$totalPrice.”)";
echo $query;

if ($result != null) {
$result->free();
$db->close();
}
?>

bakery.sql create table Product (productid int Auto_increment Primary key, ProductName char(20) not null,
		UnitPrice float(4,2) not null)

;

insert into Product values
(1,“Chocolate Cake”, 6.99),
(2,“Danish Rings”,5.49),

(3,"Cinnamon Rolls", 3.59),

(4,“Apple Pie”, 3.99);

create table orders(lastName char(20) not null, orderId int Auto_increment primary key, totalPrice float(6,2) not null);

create table order_items(productName char(20), orderId int not null, Price float(6,2) not null, quantity int not null);

create table customer(firstName char(20), lastName char(20), loginName char(20));

I am getting the error (Notice: Undefined index: quantity in C:\wamp\www\bakery\BakeryNewOrderConfirm.php on line 31) in the bakeryneworderconfirm.php

Any help on this is greatly appreciated.

Your array does not have an index quantity in it.
Show line 31

Sponsor our Newsletter | Privacy Policy | Terms of Service