himesh Posted April 9, 2010 Share Posted April 9, 2010 The final step in my shopping cart will enable users to add/remove single quantities and delete an item they no longer want. I have set up a href on the + and - which is assigned to an action. This is the shopping cart. <?php mysql_data_seek($cart,0); ?> <?php do { ?> <tr> <td><?php echo $row_cart['manufacturer']; ?></td> <td><?php echo $row_cart['modelnumber']; ?></td> <td><a href="basket.php?action=addone">+</a</td> <td><?php echo $row_cart['quantity']; ?> <input name="hidprodID" type="hidden" value="<?php echo $row_cart['productID']; ?>"> <td><a href="basket.php?action=removeone">-</a</td> </td> <td><?php echo $row_cart['price']; ?></td> <td><a href="basket.php?action=delete">Delete</a</td> </td> </tr> <?php } while ($row_cart = mysql_fetch_assoc($cart)); ?> Here are the actions that the cart is trying to call. $action =$_GET['action'];if ($action == 'addone') { if ($row_cart['quantity'] < $row_searchbar['quantity']) { $add = mysql_query("UPDATE tblcart SET quantity = `quantity` + 1 WHERE hidprodID = '$row_cart[productID]'"); }else { header( 'Location: http://www.himeshs.co.uk/communicate/sorrystock.php' ); }}if ($action == 'removeone') { if ($row_cart['quantity'] > 0) { $remove = mysql_query("UPDATE tblcart SET quantity = `quantity` - 1 WHERE hidprodID = '$row_cart[productID]'"); } else { $delete = mysql_query("DELETE FROM tblcart WHERE hidprodID = '$row_cart[productID]'"); }}if ($action == 'delete') { $delete = mysql_query("DELETE FROM tblcart WHERE hidprodID = '$row_cart[productID]'");} Link to comment Share on other sites More sharing options...
ShadowMage Posted April 9, 2010 Share Posted April 9, 2010 Can you explain the problem? Link to comment Share on other sites More sharing options...
himesh Posted April 9, 2010 Author Share Posted April 9, 2010 When I click the href, that bit works as it should as the address changes to whatever it should change to but then the actions which I have set out do not execute. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 13, 2010 Share Posted April 13, 2010 You have several if statements, check to make sure that the data you're looking for in the if statements is what you expect it to be. Link to comment Share on other sites More sharing options...
himesh Posted April 13, 2010 Author Share Posted April 13, 2010 After reading back through the code, I came to the conclusion that it was not picking the productID of whatever needed to be changed, I have since changed my code to this but it is still not working although now it is picking up the productID too. if ($action == 'removeone') { if ($row_cart['quantity'] > 0) { $remove = mysql_query("UPDATE tblcart SET quantity = `quantity` - 1 WHERE productID == '$row_cart[productID]'"); } else { $delete = mysql_query("DELETE FROM tblcart WHERE productID == '$row_cart[productID]'"); }}if ($action == 'delete') { $delete = mysql_query("DELETE FROM tblcart WHERE productID == '$row_cart[productID]'");} <?php mysql_data_seek($cart,0); ?> <?php do { ?> <tr> <td><?php echo $row_cart['manufacturer']; ?></td> <td><?php echo $row_cart['modelnumber']; ?></td> <td><a href="basket.php?productID=<?php echo $row_cart['productID'];?>&action=addone">+</a></td> <td><?php echo $row_cart['quantity']; ?> <input name="hidprodID" type="hidden" value="<?php echo $row_cart['productID']; ?>"></td> <td><a href="basket.php?productID=<?php echo $row_cart['productID'];?>&action=removeone">-</a></td> </td> <td><?php echo $row_cart['price']; ?></td> <td><a href="basket.php?productID=<?php echo $row_cart['productID'];?>&action=delete">Delete</a></td> </td> </tr> <?php } while ($row_cart = mysql_fetch_assoc($cart)); ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 13, 2010 Share Posted April 13, 2010 The advice is the same, check your data. Is $action what you think it is? Is $row_cart['quantity'] what you think it is? Link to comment Share on other sites More sharing options...
himesh Posted April 14, 2010 Author Share Posted April 14, 2010 I have managed to get the quantity to increase but it does not know why it is doing it as it sees the row_cart quantity as 1 and row product quantity as 0. But the table is actually using the same row cart quantity to display the table. I also made another table to verify that the product query was working and it does. I have tried a data seek on both of the queries to reset them back to 0 again but that did not work.The remove one query automatically goes to the else part of it and removes it. The delete however works as it should. When i try to print the query out, I keep getting an error, no matter where I place the print line. if ($action == 'addone') { if ($row_cart['quantity'] < $row_product['quantity']) { $add = mysql_query("UPDATE tblcart SET quantity = `quantity` + 1 WHERE productID = '$row_product[productID]'"); } else { header( 'Location: http://www.himeshs.co.uk/communicate/sorrystock.php' ); }}if ($action == 'removeone') { if ($row_cart['quantity'] > 0) { $remove = mysql_query("UPDATE tblcart SET quantity = `quantity` - 1 WHERE productID = '$row_product[productID]'"); } else { $delete = mysql_query("DELETE FROM tblcart WHERE productID = '$row_product[productID]'"); echo $delete; }}if ($action == 'delete') { $delete = mysql_query("DELETE FROM tblcart WHERE productID = '$row_product[productID]'"); echo $delete;} Link to comment Share on other sites More sharing options...
justsomeguy Posted April 14, 2010 Share Posted April 14, 2010 What is the error? Link to comment Share on other sites More sharing options...
himesh Posted April 14, 2010 Author Share Posted April 14, 2010 Parse error: syntax error, unexpected T_VARIABLE. This is when using print, print_r and echo Link to comment Share on other sites More sharing options...
ShadowMage Posted April 14, 2010 Share Posted April 14, 2010 Are you missing a semicolon somewhere? What line does it point to, in relation to print_r and the echo statement? Or is it always the same line no matter where you place those statements? Link to comment Share on other sites More sharing options...
himesh Posted April 15, 2010 Author Share Posted April 15, 2010 The error is wherever I put the print line. It only appears when I put the print line in too. Link to comment Share on other sites More sharing options...
ShadowMage Posted April 15, 2010 Share Posted April 15, 2010 Why don't you post a portion of the code with your print_r and echo lines in it. That way we can see what's going on. You may be missing a semicolon as I stated before or have some incorrect syntax when you call print_r or some other minor error. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 15, 2010 Share Posted April 15, 2010 A parse error always means you wrote the code wrong, we wouldn't be able to fix that without seeing the code in question. Link to comment Share on other sites More sharing options...
himesh Posted April 15, 2010 Author Share Posted April 15, 2010 I have put it at the bottom of my table. I also tried putting them at the end of their respective queries, the same error occured. <table border="0" cellpadding="0"> <tr> <td>manufacturer</td> <td>modelnumber</td> <td></td> <td>quantity</td> <td></td> <td>price</td> </tr> <?php mysql_data_seek($cart,0); ?> <?php do { ?> <tr> <td><?php echo $row_cart['manufacturer']; ?></td> <td><?php echo $row_cart['modelnumber']; ?></td> <td><a href="basket.php?productID=<?php echo $row_cart['productID'];?>&action=addone">+</a></td> <td><?php echo $row_cart['quantity']; ?> <input name="hidprodID" type="hidden" value="<?php echo $row_cart['productID']; ?>"></td> <td><a href="basket.php?productID=<?php echo $row_cart['productID'];?>&action=removeone">-</a></td> </td> <td><?php echo $row_cart['price']; ?></td> <td><a href="basket.php?productID=<?php echo $row_cart['productID'];?>&action=delete">Delete</a></td> </td> </tr> <?php } while ($row_cart = mysql_fetch_assoc($cart)); ?> <tr> <td colspan="3"> Delivery </td> <td> £5.00 <tr> <td colspan="3"> Total Price </td> <td> <?php echo $total; ?> </td> </tr> </td> <tr> <td> <?php print $add; print $remove; print $delete; ?> </td> </tr> </tr> </table> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 15, 2010 Share Posted April 15, 2010 I don't see an error there, which line there does it indicate the error is on? Link to comment Share on other sites More sharing options...
himesh Posted April 15, 2010 Author Share Posted April 15, 2010 Ok, now it is running with no errors and finally it is printing out the rows.It thinks that $row_cart['quantity']; is 1 when it should be 24 now.It also thinks that print $row_product['quantity']; is 0 when it should be 10.That is when the addone function is being called.When the remove one function is called, it still thinks the quantity is 1 and if pressed twice will completely remove the item from the cart.I have tried using the data seek again but that did not work. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 15, 2010 Share Posted April 15, 2010 Are you verifying the data? PHP doesn't "think" anything, if you have a variable in an if statement it's going to look at the value of that variable, that's it. If it "thinks" the value is 1, then the value is 1, regardless of what you think it should be. Link to comment Share on other sites More sharing options...
himesh Posted April 15, 2010 Author Share Posted April 15, 2010 I printed the values to the screen and it printed those values for me. It retrieves the correct results beforehand but then cannot do it once the function has been called even if i use a dataseek. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 16, 2010 Share Posted April 16, 2010 Which function? If you're using a function to work with the variables you've already created, you either need to pass the variables to the function or access them from the function as global variables. Link to comment Share on other sites More sharing options...
himesh Posted April 16, 2010 Author Share Posted April 16, 2010 Ok for the addone function, I created two variables and echoed them out. It sees the cart quantity as being 1(incorrect) and the product quantity as being 10 (which is correct). That is why it keeps adding as the statement will never be true. The remove one is still going down the else route. $action =$_GET['action'];$cartquan = $row_cart['quantity'];$prodquan = $row_product['quantity'];echo $cartquan;echo $prodquan;if ($action == 'addone') { if ($cartquan < $prodquan) { $add = mysql_query("UPDATE tblcart SET quantity = `quantity` + 1 WHERE productID = '$row_product[productID]'"); } else { header( 'Location: http://www.himeshs.co.uk/communicate/sorrystock.php' ); }}if ($action == 'removeone') { if ($cartquan > 0) { $remove = mysql_query("UPDATE tblcart SET quantity = `quantity` - 1 WHERE productID = '$row_product[productID]'"); } else { $delete = mysql_query("DELETE FROM tblcart WHERE productID = '$row_product[productID]'"); }}if ($action == 'delete') { $delete = mysql_query("DELETE FROM tblcart WHERE productID = '$row_product[productID]'"); } Link to comment Share on other sites More sharing options...
justsomeguy Posted April 16, 2010 Share Posted April 16, 2010 If the quantity is 1 and you think that's incorrect then you need to figure out why $row_cart['quantity'] is 1 instead of whatever you think it should be. I'm not sure what else to say about that. The remove one is still going down the else route.So you're saying that $cartquan is 1, but if ($cartquan > 0) is false? That doesn't make sense. Link to comment Share on other sites More sharing options...
himesh Posted April 16, 2010 Author Share Posted April 16, 2010 I have solved it! I thought that it couldnt find the coresponding quantity so made another sql query which retrieved all from the cart where the username and the productID was equal to the productID that the user clicked. mysql_select_db($database_conn_comm, $conn_comm);$query_cartuser = sprintf("SELECT * FROM tblcart WHERE productID = %s AND username = %s", GetSQLValueString($colname_product_cartuser, "int"),GetSQLValueString($colname_username_cartuser, "text"));$cartuser = mysql_query($query_cartuser, $conn_comm) or die(mysql_error());$row_cartuser = mysql_fetch_assoc($cartuser);$totalRows_cartuser = mysql_num_rows($cartuser); Then assigned the different fields to variables and compared them against each other and this now works! $action =$_GET['action'];$modnum = $row_cartuser['modelnumber'];$prod = $row_cartuser['productID'];$quan = $row_cartuser['quantity'];$user = $row_cartuser['username'];$prodquan = $row_product['quantity'];if ($action == 'addone') { if ($quan < $prodquan) { $add = mysql_query("UPDATE tblcart SET quantity = `quantity` + 1 WHERE productID = '$row_product[productID]'"); } else { header( 'Location: http://www.himesh.net/communicate/sorrystock.php' ); }}if ($action == 'removeone') { if ($quan > 0) { $remove = mysql_query("UPDATE tblcart SET quantity = `quantity` - 1 WHERE productID = '$row_product[productID]'"); } else { $delete = mysql_query("DELETE FROM tblcart WHERE productID = '$row_product[productID]'"); }}if ($action == 'delete') { $delete = mysql_query("DELETE FROM tblcart WHERE productID = '$row_product[productID]'"); } The only problem I have now is now when the cart gets echo'd on to the screen, the + and - links get printed on a row above where they should start. <table border="0" cellpadding="0"> <tr> <td>manufacturer</td> <td>modelnumber</td> <td></td> <td>quantity</td> <td></td> <td>price</td> </tr> <?php mysql_data_seek($cart,0); ?> <?php do { ?> <tr> <td><?php echo $row_cart['manufacturer']; ?></td> <td><?php echo $row_cart['modelnumber']; ?></td> <td><a href="basket.php?productID=<?php echo $row_cart['productID'];?>&action=addone">+</a></td> <td><?php echo $row_cart['quantity']; ?> <input name="hidprodID" type="hidden" value="<?php echo $row_cart['productID']; ?>"></td> <td><a href="basket.php?productID=<?php echo $row_cart['productID'];?>&action=removeone">-</a></td> </td> <td><?php echo $row_cart['price']; ?></td> <td><a href="basket.php?productID=<?php echo $row_cart['productID'];?>&action=delete">Delete</a></td> </td> </tr> <?php } while ($row_cart = mysql_fetch_assoc($cart)); ?> <tr> <td colspan="3"> Delivery </td> <td> £5.00 <tr> <td colspan="3"> Total Price </td> <td> <?php echo $total; ?> </td> </tr> </td> </tr> </table> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 16, 2010 Share Posted April 16, 2010 Instead of the do..while loop, use a regular while loop.while ($row_cart = mysql_fetch_assoc($cart)){...}The first time the do loop executes $row_cart might not be set, so it would print a bunch of blank values and the two links. Link to comment Share on other sites More sharing options...
himesh Posted April 16, 2010 Author Share Posted April 16, 2010 Thanks, that worked perfectly. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.