svgmx5 Posted December 11, 2009 Share Posted December 11, 2009 Last night i was working on a script for a cart and it seemed to working good, until this morning. For some reason i'ts giving me the following error: Warning: Invalid argument supplied for foreach() in /home/content/b/r/o/bronikov/html/cart/cart.php on line 29 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/b/r/o/bronikov/html/cart/cart.php on line 36 I've looked it over and over again and can't find out whats the issue. Whats weird about it is that it was working last night and i haven't changed a thing from it . Anyway here is the block of code i'm having isues with. I hope someone here can point out what the issue is Thanks <?php $sql="SELECT * FROM products WHERE productID IN ("; foreach($_SESSION['cart'] as $productID => $value) { $sql.=$productID.","; } $sql=substr($sql, 0, -1).")"; $query=mysql_query($sql); $totalprice=0; while($row=mysql_fetch_array($query)){ $subtotal=$_SESSION['cart'][$row['productiD']]['quantity']*$row['productPrice']; $totalprice+=$subtotal; ?> Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/ Share on other sites More sharing options...
rajivgonsalves Posted December 11, 2009 Share Posted December 11, 2009 why do you want a foreach in the first place <?php $sql="SELECT * FROM products WHERE productID IN (". implode(',', array_keys($_SESSION['cart']) . ")"; $query=mysql_query($sql); $totalprice=0; while($row=mysql_fetch_array($query)){ $subtotal=$_SESSION['cart'][$row['productiD']]['quantity']*$row['productPrice']; $totalprice+=$subtotal; ?> Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975586 Share on other sites More sharing options...
svgmx5 Posted December 11, 2009 Author Share Posted December 11, 2009 well at the time that was the only way i knew of how to do it. And i mean like i said it was working great last night, then today every time i click add product it takes me to the cart and gives me that error I also tried the code you posted right now, and it gives me the following error: Parse error: syntax error, unexpected ';' in cart.php on line 27 Then i noticed that it was missing a closing parenthesis and added it and now i get the following errors: Warning: array_keys() [function.array-keys]: The first argument should be an array in /cart.php on line 27 Warning: implode() [function.implode]: Invalid arguments passed in /cart.php on line 27 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /cart.php on line 31 Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975593 Share on other sites More sharing options...
rajivgonsalves Posted December 11, 2009 Share Posted December 11, 2009 your cart variable is empty do you have session_start on that page ? Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975603 Share on other sites More sharing options...
svgmx5 Posted December 12, 2009 Author Share Posted December 12, 2009 yea i have the session_start at the beginning. I'm still stuck, so far i haven't been able to get it to work. I'm still using the old code, although i made some changes. Now i'm getting the following error: Warning: Invalid argument supplied for foreach() in /home/content/b/r/o/bronikov/html/cart/cart.php on line 29 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 2 My new block of code i have is the following: <?php $sql = "SELECT * FROM product WHERE productID IN("; foreach($_SESSION['cart'] as $productID => $value){ $sql.=$productID.","; } $sql = substr($sql, 0, -1)." )"; $run = mysql_query($sql) or die(mysql_error()); $totalprice = 0; while($row = mysql_fetch_assoc($run)){ $subtotal = $_SESSION['cart'][$row['productID']]['quantity']*$row['productPrice']; $totalprice += $subtotal; ?> Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975857 Share on other sites More sharing options...
rajivgonsalves Posted December 12, 2009 Share Posted December 12, 2009 what do you get if you put var_dump($_SESSION['cart']) just before your for loop ? Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975876 Share on other sites More sharing options...
svgmx5 Posted December 12, 2009 Author Share Posted December 12, 2009 Not sure were to put it, so i went ahead and put it just above the foreach loop and i got the following Parse error: syntax error, unexpected T_FOREACH in /home/content/b/r/o/bronikov/html/cart/cart.php on line 29 And the code looks like this $sql = "SELECT * FROM product WHERE productID IN("; var_dump($_SESSION['cart']) foreach($_SESSION['cart'] as $productID => $value){ $sql.=$productID.","; } $sql = substr($sql, 0, -1)." )"; $run = mysql_query($sql) or die(mysql_error()); $totalprice = 0; while($row = mysql_fetch_assoc($run)){ $subtotal = $_SESSION['cart'][$row['productID']]['quantity']*$row['productPrice']; $totalprice += $subtotal; Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975879 Share on other sites More sharing options...
rajivgonsalves Posted December 12, 2009 Share Posted December 12, 2009 your missing the semicolin at the end of the var_dump line, all php lines have to end with a semicolin unless it is continued on the next line Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975883 Share on other sites More sharing options...
svgmx5 Posted December 12, 2009 Author Share Posted December 12, 2009 Opps my bad Okay well now i'm getting something it says NULL followed by the error here it is: NULL Warning: Invalid argument supplied for foreach() in /home/content/b/r/o/bronikov/html/cart/cart.php on line 29 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 2 So I'm assuming that my session is empty, which is why i'm not getting anything to show? Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975888 Share on other sites More sharing options...
rajivgonsalves Posted December 12, 2009 Share Posted December 12, 2009 check your session by using print_r see what is in it. Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975892 Share on other sites More sharing options...
svgmx5 Posted December 12, 2009 Author Share Posted December 12, 2009 Just getting a number 1.... Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975896 Share on other sites More sharing options...
svgmx5 Posted December 12, 2009 Author Share Posted December 12, 2009 okay so i tested it on my products info page were i have a single product displayed, and everytime i pres add to cart the array gets updated So i get the following code output: Array ( [2] => Array ( [quantity] => 3 [price] => 100 ) ) 1 Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975897 Share on other sites More sharing options...
rajivgonsalves Posted December 12, 2009 Share Posted December 12, 2009 is that your session print or session['cart'] print_r ?, also post your current code your working with Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975900 Share on other sites More sharing options...
svgmx5 Posted December 12, 2009 Author Share Posted December 12, 2009 thats from $_SESSION['cart'] Here is the code that i have for the cart <tr> <td colspan="4"><?php echo print_r($_SESSION['cart']); ?></td> </tr> <tr> <td colspan="4"><hr/></td> </tr> <?php $sql = "SELECT * FROM product WHERE productID IN("; var_dump($_SESSION['cart']); foreach($_SESSION['cart'] as $productID => $value){ $sql.=$productID.","; } $sql = substr($sql, 0, -1)." )"; $run = mysql_query($sql) or die(mysql_error()); $totalprice = 0; while($row = mysql_fetch_assoc($run)){ $subtotal = $_SESSION['cart'][$row['productID']]['quantity']*$row['productPrice']; $totalprice += $subtotal; echo print_r($_SESSION['cart']); ?> Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-975901 Share on other sites More sharing options...
svgmx5 Posted December 12, 2009 Author Share Posted December 12, 2009 okay, so its working now, i have no clue what i did, but it seems to working the items are appearing on my cart now, the only issue is when i update the quantity to zero on the last item so it can be deleted it gets deleted but 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 2 Still trying to figure out that one. Anyway Here it is the whole code i'm working with right now <?php if(isset($_POST['update'])){ foreach($_POST['quantity'] as $key => $val){ if($val==0){ unset($_SESSION['cart'][$key]); }else{ $_SESSION['cart'][$key]['quantity']=$val; } } } ?> <form method="post" action="index.php?page=cart"> <table width="840" border="0" cellpadding="0" cellspacing="0" id="cartCheckoutTable"> <tr> <td colspan="4"><h1>Your Shopping Cart</h1></td> </tr> <tr> <td width="191"><p><strong>Product</strong></p></td> <td width="381"><p><strong>Description</strong></p></td> <td width="60"><p><strong>Quantity</strong></p></td> <td width="103"><p><strong>Price</strong></p></td> </tr> <tr> <td colspan="4"><hr/></td> </tr> <?php $sql = "SELECT * FROM products WHERE productID IN("; foreach($_SESSION['cart'] as $productID => $value){ $sql.=$productID.","; } $sql = substr($sql, 0, -1)." )"; $run = mysql_query($sql) or die(mysql_error()); $totalprice = 0; while($row = mysql_fetch_assoc($run)){ $subtotal = $_SESSION['cart'][$row['productID']]['quantity']*$row['productPrice']; $totalprice += $subtotal; echo print_r($_SESSION['cart']); ?> <tr> <td><p><?php echo $row['productTitle']?></p></td> <td><?php echo $row['productDescription'] ?></td> <td><input type="text" name="quantity[<?php echo $row['productID'] ?>]" value="<?php echo $_SESSION['cart'][$row['productID']]['quantity']?>"/></p></td> <td><p><?php echo $row['productPrice']?></p></td> </tr> <tr> <td colspan="4"><hr/></td> </tr> <?php } ?> <tr> <td colspan="3"><p class="subtotalTxt">Subtotal:</p></td> <td><p><?php echo ''.$totalprice.'' ;?></p></td> </tr> <tr> <td colspan="4"> <input type="submit" name="update" value="Update Cart" /> </td> </tr> </table> </form> Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-976236 Share on other sites More sharing options...
rajivgonsalves Posted December 13, 2009 Share Posted December 13, 2009 You should print out your sql statement and try to run that in phpmyadmin also seeing your code you can change the select to $sql = "SELECT * FROM products WHERE productID IN(" . implode(',', array_keys($_SESSION['cart'])) . ")"; which I feel should solve your problem Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-976345 Share on other sites More sharing options...
svgmx5 Posted December 13, 2009 Author Share Posted December 13, 2009 okay i tried that and changed the code and that still didn't work. i'm still getting the 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 This only displays if there are no items in the session and when all the items in the cart have been deleted. Also when i try to add a product to the cart i get this message instead of adding them to the cart Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-976401 Share on other sites More sharing options...
rajivgonsalves Posted December 13, 2009 Share Posted December 13, 2009 you could just put a if to check if there are items in your cart and if there are process the query otherwise do nothing Link to comment https://forums.phpfreaks.com/topic/184802-having-a-small-php-error-tha-deals-with-the-foreach/#findComment-976648 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.