thelee Posted July 7, 2014 Share Posted July 7, 2014 i make a shopping cart,but it dont show the product i have choose. it suppose to show the product i choose at the cart table but its still show the "Your Cart is empty" result..im still new.i follow the instruction from the youtube.sorry for my bad english. here is the index2.php code <? session_start(); require("connection.php"); if(isset($_GET['page'])){ $pages=array("products", "cart"); if(in_array($_GET['page'], $pages)) { $_page=$_GET['page']; }else{ $_page="products"; } }else{ $_page="products"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <body> </body> </html> <link rel="stylesheet" href="style2.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script> <script type="text/javascript"> $(function() { }); </script> </head> <body> <div id="container"> <div id="main"> <?php require($_page.".php"); ?> </div> <div id="sidebar"> <h1>Cart</h1> <?php if(isset($_SESSION['cart'])){ $sql="SELECT * FROM products WHERE id_product IN ("; foreach($_SESSION['cart'] as $id => $value) { $sql.=$id.","; } $sql=substr($sql, 0, -1).") ORDER BY name ASC"; $query=mysql_query($sql); while($row=mysql_fetch_array($query)){ ?> <p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p> <?php } }else{ echo "<p>Your Cart is empty</p>"; } ?> </div> </div> </body> and this is the products.php code <?php if(isset($_GET['action']) && $_GET['action']=="add"){ $id=intval($_GET['id']); if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]['quantity']++; }else{ $sql_s="SELECT * FROM products WHERE id_product={$id}"; $query_s=mysql_query($sql_s); if(mysql_num_rows($query_s)!=0){ $row_s=mysql_fetch_array($query_s); $_SESSION['cart'][$row_s['id_product']]=array( "quantity" => 1, "price" => $row_s['price'] ); }else{ $message="This product id it's invalid!"; } } } ?> <h1>Product List</h1> <?php if(isset($message)){ echo "<h2>$message</h2>"; } ?> <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); while ($row=mysql_fetch_array($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_product'] ?>">Add To Cart</a></td> </tr> <?php } ?> </table> Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 7, 2014 Share Posted July 7, 2014 Do you get any errors? Echo out the $sql before running the query on the index2 page and see if the query string looks correct and has the product ids that you expect it too. Obviously the query if failing to return anything on index2 cause it's telling you the cart is empty. So either the query is failing cause the $sql syntax is wrong or there are no matches for the query string provided. Also this line $id=intval($_GET['id']); Would be better like this $id=(int)$_GET['id']; // This type casts it to a integer. Even if it's been compromised by someone it will still return a 0 which will not make your query fail on the other page. 1 Quote Link to comment Share on other sites More sharing options...
thelee Posted July 7, 2014 Author Share Posted July 7, 2014 i have this error when i try running the query Notice: Undefined index: cart inC:\xampp\htdocs\emakengku\products.php on line 50 Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 7, 2014 Share Posted July 7, 2014 Ok well that makes no sense to the code you provided, cause there isn't any variable close to line 50. Are you sure the code ou provided is the entire code for the page? If it is then you need to tell us exactly what bit of code the error is referring to since it doesn't match up to line 50 for us. 1 Quote Link to comment Share on other sites More sharing options...
thelee Posted July 7, 2014 Author Share Posted July 7, 2014 i replace this code with the products.php to try it.and it give me that error. <?php if(isset($_GET['action']) && $_GET['action']=="add"){ $id=intval($_GET['id']); if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]['quantity']++; }else{ $sql_s="SELECT * FROM products WHERE id_product={$id}"; $query_s=mysql_query($sql_s); if(mysql_num_rows($query_s)!=0){ $row_s=mysql_fetch_array($query_s); $_SESSION['cart'][$row_s['id_product']]=array( "quantity" => 1, "price" => $row_s['price'] ); }else{ $message="This product id it's invalid!"; } } } ?> <h1>Product List</h1> <?php if(isset($message)){ echo "<h2>$message</h2>"; } echo print_r($_SESSION['cart']); ?> <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); while ($row=mysql_fetch_array($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_product'] ?>">Add To Cart</a></td> </tr> <?php } ?> </table> Quote Link to comment Share on other sites More sharing options...
thelee Posted July 7, 2014 Author Share Posted July 7, 2014 i follow the step from here http://www.youtube.com/watch?v=gqfy9pcKwgE Quote Link to comment Share on other sites More sharing options...
Solution fastsol Posted July 7, 2014 Solution Share Posted July 7, 2014 I think I found your error. You had a $ instead of & in your link to Add the product. <a href="index2.php?page=products&action=add$id=<?php echo $row['id_product'] ?>">Add To Cart</a> Should be this <a href="index2.php?page=products&action=add&id=<?php echo $row['id_product'] ?>">Add To Cart</a> So it wasn't reading the $_GET['id'] at all. 1 Quote Link to comment Share on other sites More sharing options...
thelee Posted July 7, 2014 Author Share Posted July 7, 2014 yeah! u solved it mann ! i love u so much but not in a gay mood ! u are my hero.hahaha.muahhh Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.