So, I'm trying to create a simple shopping cart for my php class. I've come to a stop because I noticed the product IDs aren't working the correct way. Right now, I have an info button that should pull up information from a database, but it seems it keeps pulling the same information for every product I select. I have 20 products, with a ProductID column going from 1-20, but it almost seems like it assigns every product to "1". For now, I just want to pull up the info when I hit the info button, as I'm sure solving that will help with the rest. Here is the code:
<?php
session_start();
require_once 'DBLogin.php';
echo "<form action='Catalogue.php' method='Post'>";
$sql = "SELECT * FROM ACME.Products";
$result = mysql_query($sql);
echo "<select name='Select_Product'>";
while ($row = mysql_fetch_array($result)) {
echo "Please select a product: <option value='" . $row['ProductID'] . "'>" . $row['Name'] . "</option>";
}
echo "</select>";
echo <<<HTML
<table>
<tr> <td>
<input id="button_Add" type="submit" value="Add" name="action"/>
</td> <td> <input id="button_Remove"
type="submit" value="Remove" name="action"/>
</td> <td> <input id="button_empty"
type="submit" value="Empty" name="action"/>
</td> <td> <input id="button_Info"
type="submit" value="Info" name="action"/>
</td> </tr>
</table>
</form>
HTML;
//code to attempt to pull the info
$product_id = $_POST['Select_Product'];
$action = $_POST['action'];
switch ($action) {
case "Add":
echo $_SESSION['cart'][$product_id]++;
break;
case "Remove":
$_SESSION['cart'][$product_id]--;
if($_SESSION['cart'][$product_id] == 0)
unset($_SESSION['cart'][$product_id]);
break;
case "Empty":
unset($_SESSION['cart']);
break;
case "Info":
$sql = "SELECT * FROM ACME.Products";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "<table>";
echo "<tr><td>Name</td><td>Description</td ><td>Price</td><td>Image</td> </tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>". $row['Description'] . "</td>";
echo "<td>". $row['Price'] . "</td>";
echo "<td align=\"center\"><img alt=\"\" src=\"productImages/". $product_id . ".jpg\ width=\"120\" height=\"120\"/></td></tr>";
echo "</table>";
break;
}
Here is the link to the site so you can see what's going on too: http://ec2-52-24-105-81.us-west-2.compute.amazonaws.com/Catalogue.php
Any help would be greatly appreciated!
you forgot where
clause
case "Info":
$sql = "SELECT * FROM ACME.Products Where id = $product_id Limit 1";