Jump to content

having a small php error tha deals with the 'foreach'


svgmx5

Recommended Posts

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
Share on other sites

why do you want a foreach in the first place :P

 

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.