svgmx5 Posted February 23, 2010 Share Posted February 23, 2010 Hi everyone I'm having some problems with my cart script, and i'm hoping maybe someone here can help me out here's what its doing Everytime i clear the cart, or well remove all the items from it i get the following error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 I also get the following error message when there are no items on the cart session and i just click update Warning: Invalid argument supplied for foreach() in /home/content/w/a/r/warbudz10/html/scripts/php/addtoCart.php on line 35 I've looked over the code over and over again, and i just can't figure out whats wrong. I hope someone that looks over this can help me out Anyway, here's the code that i'm using to add items and update the cart <?php if(isset($_GET['action']) && $_GET['action']=="add"){ $productID=intval($_GET['productID']); if(isset($_SESSION['cart'][$productID])){ $_SESSION['cart'][$productID]['quantity']++; }else{ $sql = "SELECT * FROM products WHERE productID={$productID}"; $run = mysql_query($sql) or die (mysql_error()); $num = mysql_num_rows($run); if($num!=0){ while($fetch = mysql_fetch_array($run)){ $productID = $fetch['productID']; $productPice = $fetch['productPrice']; } $_SESSION['cart'][$productID]=array( "quantity" => 1, "price" => $productPice ); }else{ $message="This product id it's invalid!"; } } } if(isset($_POST['update'])){ foreach($_POST['quantity'] as $key => $val){ if($val==0){ unset($_SESSION['cart'][$key]); }else{ $_SESSION['cart'][$key]['quantity']=$val; } } } ?> And also here's t he code that i'm using to display the shoping cart <div id="mainsideCartBody"> <form action="product.php?productID=<?php echo ' '.$pID.' ' ; ?>" method="post"> <table width="240" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="96" colspan="2"><p>Item name</p></td> <td width="79"><p>Price</p></td> </tr> <?php if(isset($_SESSION['cart'])){ $sql = "SELECT * FROM products WHERE productID IN(" . implode(',', array_keys($_SESSION['cart'])) . ")"; $query = mysql_query($sql) or die(mysql_error()); $num = mysql_num_rows($run); $totalprice = 0; while($row=mysql_fetch_array($query)){ $subtotal = $_SESSION['cart'][$row['productID']]['quantity']*$row['productPrice']; $totalprice += $subtotal; ?> <tr> <td colspan="2"> <p><?php echo $row['productName'] ?></p> </td> <td> <p>$<?php echo $row['productPrice']?> x <input name="quantity[<?php echo $row['productID'] ?>]" type="text" value="<?php echo $_SESSION['cart'][$row['productID']]['quantity']?>" size="3" maxlength="3"/> </p> </td> </tr> <?php } ?> <tr> <td colspan="3"> <hr/> </td> </tr> <tr> <td colspan="2"> <h6>Subtotal:</h6> </td> <td> <h6>$<?php echo ' '.$totalprice.' ' ;?></h6> </td> </tr> <?php }else{ echo " <tr> <td colspan='3'><h1>Your Cart is empty. Please add some products.</h1></td> </tr> "; } ?> <tr> <td colspan="3"> <input type="submit" value="Update" name="update" /> <a href="product.php?action=update&productID=<?php echo ' '.$pID.' ' ;?>"> checkOut </a> </td> </tr> </table> </form> </div> Link to comment https://forums.phpfreaks.com/topic/193062-problems-updating-the-items-on-my-php-cart/ Share on other sites More sharing options...
veridicus Posted February 23, 2010 Share Posted February 23, 2010 If your $_SESSION['cart'] is empty, you'll get a query error because "IN ()" is invalid. You should change if(isset($_SESSION['cart'])){ to if(isset($_SESSION['cart']) && $_SESSION['cart']){ or something similar Link to comment https://forums.phpfreaks.com/topic/193062-problems-updating-the-items-on-my-php-cart/#findComment-1016827 Share on other sites More sharing options...
svgmx5 Posted February 25, 2010 Author Share Posted February 25, 2010 Okay so that helped part of the issue. Now whenever i empty the cart i don't get any errors. However when the cart is empty and i click update, i get the following error: Warning: Invalid argument supplied for foreach() in /home/content/w/a/r/warbudz10/html/scripts/php/addtoCart.php on line 35 Once again here's the code <?php if(isset($_GET['action']) && $_GET['action']=="add"){ $productID=intval($_GET['productID']); if(isset($_SESSION['cart'][$productID])){ $_SESSION['cart'][$productID]['quantity']++; }else{ $sql = "SELECT * FROM products WHERE productID={$productID}"; $run = mysql_query($sql) or die (mysql_error()); $num = mysql_num_rows($run); if($num!=0){ while($fetch = mysql_fetch_array($run)){ $productID = $fetch['productID']; $productPice = $fetch['productPrice']; } $_SESSION['cart'][$productID]=array( "quantity" => 1, "price" => $productPice ); }else{ $message="This product id it's invalid!"; } } } if(isset($_POST['update'])){ foreach($_POST['quantity'] as $key => $val){ //This is Line 35 if($val==0){ unset($_SESSION['cart'][$key]); }else{ $_SESSION['cart'][$key]['quantity']=$val; } } } ?> Link to comment https://forums.phpfreaks.com/topic/193062-problems-updating-the-items-on-my-php-cart/#findComment-1017854 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.