Agreaves Posted August 23, 2012 Share Posted August 23, 2012 I created this simple shopping system but it wont show the result of the query in the table, what am I doing wrong? <?php // Product Id from URL $product_id = $_GET['Id']; // Action from URL $action = $_GET['action']; //if there is an product_id and that product_id doesn't exist display an error message if($product_id && !productExists($product_id)) { die("Error. Product Doesn't Exist"); } switch($action) { //decide what to do case "add": $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id break; case "remove": $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise it will show zero, then -1, -2 etc when the user keeps removing items. break; case "empty": unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. break; } ?> <?php if($_SESSION['cart']) { //if the cart isn't empty //show the cart echo "<table border=\"1\" padding=\"3\" width=\"40%\">"; //format the cart using a HTML table //iterate through the cart, the $product_id is the key and $quantity is the value foreach($_SESSION['cart'] as $product_id => $quantity) { //get the name, description and price from the database - this will depend on your database implementation. //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT Name, Description, Price FROM products WHERE Id = %d;", $product_id); $result = mysqli_query($con,$sql) or die("couldnt run query"); $row = mysqli_num_rows($result); $row2 = mysqli_fetch_assoc($result); $name = $row2['Name']; $description = $row2['Description']; $price = $row2['Price']; //Only display the row if there is a product (though there should always be as we have already checked) if($row > 0) { list($name, $description, $price) = mysqli_fetch_row($result); $line_cost = $price * $quantity; //work out the line cost $total = $total + $line_cost; //add to the total cost echo "<tr>"; //show this information in table cells echo "<td align=\"center\">$name</td>"; //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product echo "<td align=\"center\">$quantity <a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">X</a></td>"; echo "<td align=\"center\">$line_cost</td>"; echo "</tr>"; } } //show the total echo "<tr>"; echo "<td colspan=\"2\" align=\"right\">Total</td>"; echo "<td align=\"right\">$total</td>"; echo "</tr>"; //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation echo "<tr>"; echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[php_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>"; echo "</tr>"; echo "</table>"; }else{ //otherwise tell the user they have no items in their cart echo "You have no items in your shopping cart."; } //function to check if a product exists function productExists($product_id) { //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection $sql = sprintf("SELECT * FROM php_shop_products WHERE id = %d;", $product_id); return mysql_num_rows(mysql_query($sql)) > 0; } ?> Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 23, 2012 Share Posted August 23, 2012 Hi Agreaves, your topic sounds to me like - How to make a good song? I don't want to be rude, but you posted here tons of code without to make even little debugging and expect from us to give you a correct answer. My question is, have you tried dumping the code, if so what errors did you get? Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 23, 2012 Author Share Posted August 23, 2012 I dont know how to dump the code, im not receiving any error messages, it seems to run the query properly but right where it is suppose to echo with result instead it echos a blank table. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 Turn on error reporting set to E_ALL. Also show us the output of the generated page. I'm going to guess $row=0, so it's not getting into the cells part. Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 23, 2012 Author Share Posted August 23, 2012 I just did a print_r on the rows found and the result returned 0 so I dont know whats wrong with the query. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 $sql = sprintf("SELECT Name, Description, Price FROM products WHERE Id = %d;", $product_id); echo "SQL: $sql"; What's it output? Quote Link to comment Share on other sites More sharing options...
Agreaves Posted August 23, 2012 Author Share Posted August 23, 2012 the results returned "SQL: SELECT Name, Description, Price FROM products WHERE Id = 0;" Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 Well I can only assume you don't have any products with an ID of 0 right? When you added the products to your cart in the session, you set the ID incorrectly, or not at all. Go back there. And start trying to debug it yourself. Whenever you set a variable, echo it to see if it's what you expected. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.