Hi all. Im building a shopping cart using an online tutorial in youtube, but i have an error

<?php

if(isset($_GET['action']) && $_GET['action'] == "add"){
	$id = intval($_GET['id']);
	if(isset($_SESSION['cart'][$id])){
		$_SESSION['cart'][$id]['quantity']++;
		
	}	else{
		
			$sql2 = "SELECT * FROM products WHERE id_products=$id";
			$query2 = mysql_query($sql2);	
			
			if(mysql_num_rows($query2) != 0){
			$row2 = mysql_fetch_array($query2); 
		
			$_SESSION['cart'][$row2['id_products']] = array("quantity" => 1, "price" => $row2['price']);
		
	
			}else{
			
				$message = "This product id is invalid";
			}
		}
	}



?>
<h2 class="message"><?php if(isset($message)){echo $message;} ?></h2>

<h1>Product</h1>

<table>


	<tr>
    	<th>name</th>
    	<th>description</th>
    	<th>price</th>
    	<th>action</th>
	</tr>

	<?php
	$sql = "SELECT * FROM products ORDER BY name ASC";
	$query = mysql_query($sql) or die(mysql_error());
	
	while
	($row = mysql_fetch_assoc($query)){
		

	?>
    
    <tr>
    	<td><?php echo $row['name']; ?></td>
        <td><?php echo $row['description']; ?></td>
        <td><?php echo "$" .$row['price']; ?></td>
        <td><a href="index2.php?page=products&action=add&id<?php echo $row['id_products'];?>">Add to Cart</a></td>
        </tr>
        
        <?php
		}
		?>
        
      
   
       
        
    
    
		
</table>
<p>&nbsp;</p>
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in F:\root\xampplite\htdocs\shopping\product.php on line 13

On the comment section some user had this problem and he said to check if the session names are correct and to do a die message which I did but I think my session names seems fine and an error occured when I tried to put a die message.

Never the less the tutorial is the best I ever did for php.

I appreciate the help :).

Recommended Answers

All 5 Replies

Please provide a link to the tutorial in question.

Sorry about that I give you to it right now.

http://www.youtube.com/watch?v=DS1PKSzar_g&feature=related

Also I tried to be clever by trying to fix the error by changing the file name to product.php to products.php as the guy has his saved as products.php (sorry for my poor grammer)

<?php

session_start();
?>
<?php



include_once'config2.php';

if(isset($_GET['page']))//get info from a get variable inside url
{
	$pages = array("products", "cart");//define array variable
	
	if(in_array($_GET['page'], $pages))//if does not exist in array will go to defualt page
	{
		$page = $_GET['page'];
	}
	else 
	{
		$page ="products";
	}
}
else
{
	$page ="products";
}


?>


<html>
<head>

            <link rel="stylesheet" href="css/reset.css" />
           
            <title>Shopping cart </title>

    <link href="style.css" rel="stylesheet" type="text/css">
</head>
	<body>
    <div id="container">
    	<div id="main"><?php require($page. ".php"); // concatinate page that will call the defualt page?></div>
        <div id="sidebar"><h1>Cart</h1>

<?php

if(isset($_SESSION['cart'])){
	
	$sql = "SELECT * FROM products WHERE id_products IN (";
	
	foreach($_SESSION['cart'] as $id => $value) {//changes a session by the name of cart to an id within the array
		$sql .= $id. ",";//sql concatinates to the variable of  the id 
		}
	
	
	$sql = substr ($sql, 0, -1).") ORDER BY id_products ASC";//subtring will gets rid of the last comma, otherwise an error will occur 
	$query = mysql_query($sql);	
	
	
	while($row = mysql_fetch_array($query)){
?>



<p><?php echo $row['name'];?> <?php echo $_SESSION['cart'][$row ['id_products']]['quantity']; ?></p>
<a href="index2.php?page=cart">Go to Cart</a>

<?php

	}
	}else{
		
		echo "<p> Your cart is empty.<br />Please add some products</p>";
		
	}
?>
</div>
    </div>     
  </body>
</html>

error

Parse error: syntax error, unexpected ';', expecting ']' in C:\xampp\htdocs\shopping\products.php  on line 57

You may need to go back a few steps backs on his videos has he made over dozen steps.

Thanks i really do appriciate your help :D

fixed my unexpected error still have this error.

Resource id #6
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in E:\root\xampplite\htdocs\shopping\index2.php on line 62

Im not sure if the tutorial is the problem but some people on the comments seemed to get it working :s.

Wonder if anyone can help my code has been pasted on my last post

thanks

p.s sorry for the bump

Youre query doesn't give a good result
use at line 12

$query2 = mysql_query($sql2) or die(mysql_error());

to get more details

Youre query doesn't give a good result
use at line 12
$query2 = mysql_query($sql2) or die(mysql_error());

to get more details

Hey sorry for late reply thanks for this,the error does not show up but when I click on the add to cart it says

"This product id is invalid"

I know what the problem is its not finding my products_id but I know I have changed everything to products_id on the query and everything else but still can't find the solution. I did double check my databse and has the name is fine.

I paste my codes

my index.php page

<?php

if(isset($_GET['action']) && $_GET['action'] == "add"){
    $id = intval($_GET['id']);
    if(isset($_SESSION['cart'][$id])){
        $_SESSION['cart'][$id]['quantity']++;

    }   else{

            $sql2 = "SELECT * FROM products WHERE id_products='$id'";
            $query2 = mysql_query($sql2) or die(mysql_error());
            print_r($sql2);
            if(mysql_num_rows($query2) != 0)
            {
                $row2 = mysql_fetch_array($query2); 

                $_SESSION['cart'][$row2['id_products']] = array("quantity" => 1, "price" => $row2['price']);

            }
            else
            {
                $message = "This product id is invalid";
            }
        }
    }



?>
<h2 class="message"><?php if(isset($message)){echo $message;} ?></h2>

<h1>Product</h1>

<table>


    <tr>
        <th>name</th>
        <th>description</th>
        <th>price</th>
        <th>action</th>
    </tr>

    <?php
    $sql = "SELECT * FROM products ORDER BY name ASC";
    $query = mysql_query($sql) or die(mysql_error());

    while
    ($row = mysql_fetch_assoc($query)){


    ?>

    <tr>
        <td><?php echo $row['name']; ?></td>
        <td><?php echo $row['description']; ?></td>
        <td><?php echo "$" .$row['price']; ?></td>
        <td><a href="index2.php?page=products&action=add&id<?php echo $row['id_products']; ?>">Add to Cart</a></td>
        </tr>

        <?php
        }
        ?>

products.php

<?php

session_start();
?>
<?php



include_once'config2.php';

if(isset($_GET['page']))//get info from a get variable inside url
{
    $pages = array("products", "cart");//define array variable

    if(in_array($_GET['page'], $pages))//if does not exist in array will go to defualt page
    {
        $page = $_GET['page'];
    }
    else 
    {
        $page ="products";
    }
}
else
{
    $page ="products";
}


?>


<html>
<head>

            <link rel="stylesheet" href="css/reset.css" />

            <title>Shopping cart </title>

    <link href="style.css" rel="stylesheet" type="text/css">
</head>
    <body>
    <div id="container">
        <div id="main"><?php require($page. ".php"); // concatinate page that will call the defualt page?></div>
        <div id="sidebar"><h1>Cart</h1>

<?php

if(isset($_SESSION['cart'])){

    $sql = "SELECT * FROM products WHERE id_products IN (";

    foreach($_SESSION['cart'] as $id => $value) {
        $sql .= $id. ",";
        }

    $sql = substr ($sql, 0, -1).") ORDER BY id_products ASC";

    $query = mysql_query($sql); 


    while($row = mysql_fetch_array($query)){
?>



<p><?php echo $row['name'];?> <?php echo $_SESSION['cart'][$row ['id_products']]['quantity']; ?></p>
<a href="index2.php?page=cart">Go to Cart</a>

<?php

    }
    }else
    {

        echo "<p> Your cart is empty.<br />Please add some products</p>";

    }
?>
</div>
    </div>     
  </body>
</html>


</table>
<p>&nbsp;</p>

Im very confused and really want to finish the tutoriol quick as i can.

If any of you familier with print_r function that you can get to tell an hidden error I would be grateful :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.