PHP Quantity Script not working properly

I have a menu page that selects menu items from a server table and I’ve added a quantity selection to that page. I’m using a form button place the order go to a confirmation page which I want to have insert the menu items into an order table and then display those items and quantity if the selected quantity is greater than 0. I think I may be having trouble with how to assign those menu items to a variable because I can’t even echo them. Here is the relevant portion of my menu page and the entire page. Any guidance would be greatly appreciated.

menu.php:

<?php $link = mysqli_connect("localhost","jmckenzi_jmckenz","mck3nzi3","jmckenzi_customer") or die("Error " . mysqli_error($link));
	echo "<table border='1' cellpadding='1' cellspacing='1'>";
	echo "<tr>";
	echo "<th>Quantity</th>";
	echo "<th>Name</th>";
	echo "<th>Description</th>";
	echo "<th>Spicy?</th>";
	echo "<th>Price</th>";
	echo "</tr>	";

//echo APPETIZERS to html table
$queryApp = “SELECT * FROM menu WHERE itemCat = ‘APPETIZERS’” or die(“Error in the consult…” . mysqli_error($link));
$resultApp = $link->query($queryApp);
echo “

”;
echo"
";
echo (“APPETIZERS”);
		if ($resultApp->num_rows > 0) {
		// output data of each row
			while($row = $resultApp->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				echo "<td name='itemName'>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td name='isSpicy' style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td name='itemPrice' style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

//continued…

order.php:

<?php $itemPrice = $_REQUEST['itemPrice']; //Retrieve data from form and store as variables $quantity = $_REQUEST['quantity']; $itemName = $_REQUEST['itemName']; include "scripts/database_connection.php"; $query = "INSERT INTO order(itemPrice,quantity,itemName) VALUES('$itemPrice','$quantity','$itemName') WHERE '$quantity' > 0"; $result = mysqli_query($link, $query); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { $last_inserted_id = $link->insert_id; $affected_rows = $link->affected_rows; echo("Last Inserted Id: ". $last_inserted_id ); echo("Affected rows: " . $affected_rows); echo("Successfully added to the database:"); echo("
Price: {$itemPrice}
Quantity: {$quantity}
Item: {$itemName}
"); } } else { echo "0 results"; } $link->close(); //Close linkection echo("Go back to Home"); ?>

Jason, a common simple error… First, let’s recap clauses…

Note that in your code, you loop thru the appetizers and create a separate for each of them.
That is okay, but, you never name these. Therefore, you can never load the results from them. Also, you
did not set up your form with a valid name and did not tell it to use POST or GET options. You must fix these
if you want to be able to read the data back in. Normally, you set your tag to tell it to use “POST”.
Then, for each tag, you assing a name to it so that you can read it using the $_POST[“name”]
functions. You can leave off names if you wish, but, then you have to parse thru the $_POST or $_REQUEST
arrays and find each of them in a loop. Harder to handle this way. One way to fix this would be to assign
a name that is equal to the appetizer’s ID number. So, like would be the first ID in the
appetizer table. etc. You would do that by changing the code that displays the tag for each of
the items. Like instead of " it would be programmed to add in the name of each appetizers like:
echo ‘<select name="’ . $row[“ID”] . ‘">’;
This would set a different SELECT clause for each of the appetizers you have in your table. Then, when you
process then, you would loop thru all of the appetizers using the table ID’s to see which ones are not zero.
And, insert each into the ORDER table. You can not use the $_REQUEST[] table in the manner that you are
trying to do it. The $_REQUEST[] is an array of arrays. Each entry inside this is a special entry. I guess you
could parse thru it using a WHILE and read all of the entries and see which are flagged as “orderprice”,
“quantity” and “itemName”… You can not run a query like "INSERT INTO order(array[],array[],array[])…
You can loop thru the $_REQUEST[] array and find every SELECT and insert each of them separately into
the order table, but, not as one array. At least as far as I know.

TO explain a little further. If you want to add 5 orders of onion rings and 2 orders of chicken wings, you
would have to do so sort of like:
INSERT INTO order (itemPrice, quantity, itemName) VALUES(’$3.00’,‘5’,‘Onion Rings’)
INSERT INTO order (itemPrice, quantity, itemName) VALUES(’$7.00’,‘2’,‘Chicken Wings’)
Just a loose example. Currently, you are attempting to use data from the $_REQUEST[] array like this:
VALUES(’$itemPrice’,’$quantity’,’$itemName’) WHERE ‘$quantity’ > 0"
First, the WHERE clause is used to search the database, NOT the incoming data in the $_REQUEST[] array.
Also, you are attempting to store an array of data, $quantity, etc into one entry in the database. ??? Not
going to work. If you want to store an array of items, you can do that, but, you have to load it as an array.
You get the quantities with: $quantity = $_REQUEST[‘quantity’]; But, you did not define the string as an
array.

Perhaps I am a bit confused on how you want to handle this system. Normally, in an order entry system,
you would have an order table with a structure something like:
ID#, userID#, itemID, quantity
Then, when a user entered an order, you would save their ID number, the item ordered and the quantity.
The item’s name and price would be stored in the appetizer table, not actually stored in the order itself.
Then, to review the order, a query like “SELECT * FROM order WHERE userID='current user”" would pull
all of their current order info. You would also use the appetizer table to cross-referrence with each item’s
name and current price when displaying their order.

A simple way to debug all of this code is to force a display of the data itself right after the user press the
order button. Just display the $_REQUEST array and what you are pulling out for your orders. Something
like this would work… Place it just before the INSERT INTO ORDER query…
[php]
echo “RESPONSE array:
”;
print_r($_RESPONSE);
echo “
itemPrice values:
”;
print_r($itemPrice);
echo “
quantity values:
”;
print_r($quantity);
echo “
itemName values:
”;
print_r($itemName);
die("
**** DONE! ****
");
[/php]

This will display the request entries so that you can see what was actually sent back to the server from
your form along with the three variables that you loaded from that data. You can see if they are what you
really want or not. My guess is that it will not be the data you really want. If it is what you are looking for,
you just have to parse thru them, pulling out the correct index of each and inserting them each.

Not sure if all of this helps or not, but, let us know. And, please post your code inside tags next
time. This helps us test it much faster! Good luck

Sponsor our Newsletter | Privacy Policy | Terms of Service