Krux20 Posted November 27, 2012 Share Posted November 27, 2012 Hi, I kind of need help in how I can create a cart for a user, so that the user can add or delete the quantity of items in the cart. What do I need to do first? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/ Share on other sites More sharing options...
MDCode Posted November 27, 2012 Share Posted November 27, 2012 (edited) Step 1. Create the system. Step 2. Ask a question if something goes wrong. If it's for a user, store it in mysql or cookies because they can log out and lose all their cart Edited November 27, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395598 Share on other sites More sharing options...
Krux20 Posted November 27, 2012 Author Share Posted November 27, 2012 Hi, thanks for the reply and for some reason I'm getting this error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in: I don't know what the problem is. function products () { $con = mysqli_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bicycle"); $result = mysqli_query($con,"SELECT BikeCode, Model FROM bike"); while($row = mysqli_fetch_array($result)) { echo $row['BikeCode'] . " " . $row['Model']; echo " "; } Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395602 Share on other sites More sharing options...
codefossa Posted November 27, 2012 Share Posted November 27, 2012 (edited) Your query failed and it's returning false. You can't pass a boolean through the mysqli_fetch_array() function. $sql = "SELECT BikeCode, Model FROM bike"; $result = mysqli_query($con, $sql) or die("Error: Failed to query database. ({$sql})"); Edited November 27, 2012 by Xaotique Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395612 Share on other sites More sharing options...
Krux20 Posted November 27, 2012 Author Share Posted November 27, 2012 if (!$con) { die('Could not connect: ' . mysql_error()); } else { echo 'connected'; } Thanks for the reply, but nothing seems to be wrong with my database and when I try the following code it says I'm connected to my database. Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395614 Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2012 Share Posted November 27, 2012 You'll find that the error in your query is because you haven't selected a database. You cannot mix mysql (no i) and mysqli (with an i) statements. You either need to specify the database in the mysqli_connect statement or use a mysqli_select_db statement (or even specify the database name in your query statement.) Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395615 Share on other sites More sharing options...
Krux20 Posted November 27, 2012 Author Share Posted November 27, 2012 I added a new mysqli_select_db statement, but it still gives me that error and I thought it was because my MySQL service wasn't running, but it seems to working fine. function products () { $con = mysqli_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysqli_error()); } else { echo 'connected'; } $db= mysqli_select_db("bicycle"); $sql = "SELECT BikeCode, Model FROM bike"; $result = mysqli_query($db, $sql) or die("Error: Failed to query database. ({$sql})"); while($row = mysqli_fetch_array($result)) { echo $row['BikeCode'] . " " . $row['Model']; echo " "; } } Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395618 Share on other sites More sharing options...
codefossa Posted November 27, 2012 Share Posted November 27, 2012 (edited) If it's the same as mysql functions, you have to specify the connection as the first parameter in the mysql_select_db (atleast I think it's a must, never tried without). You could also double check your query in phpmyadmin to make sure it's valid. Edited November 27, 2012 by Xaotique Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395621 Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2012 Share Posted November 27, 2012 The mysqli_select_db statement requires the mysqli link resource ($con in your case) as the first parameter. Do you have php's error_reporting set to E_ALL so that all the php detected errors will be reported? Also, the mysqli_error() statement requires the mysqli link resource as a parameter. Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395624 Share on other sites More sharing options...
Krux20 Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) Yes, I have it on E_ALL. http://tinypic.com/r/e5jybk/6 function products () { $con = mysqli_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysqli_error()); } else { echo 'connected'; } $db= mysqli_select_db($con,"bicycle"); $sql = "SELECT BikeCode, Model FROM bike "; $result = mysqli_query($db, $sql) or die("Error: Failed to query database. ({$sql})"); while($row = mysqli_fetch_array($result)) { echo $row['BikeCode'] . " " . $row['Model']; echo " "; } } Edited November 27, 2012 by Krux20 Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395627 Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2012 Share Posted November 27, 2012 If you are still getting an error at your mysqli_fetch_assoc statement, with the code you have been posting, it's because some of your code inside of your while(){} loop, that you have been cutting out of the post, is reusing and overwriting the $result variable. Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395628 Share on other sites More sharing options...
Krux20 Posted November 27, 2012 Author Share Posted November 27, 2012 (edited) This is all my code <?php error_reporting(E_ALL); session_start(); $page = 'index.php'; // $_SESSION['cart'. $_GET['add']] = 1; // $_SESSION['cart'] += 1; function products () { $con = mysqli_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysqli_error()); } else { echo 'connected'; } $db= mysqli_select_db($con,"bicycle"); $sql = "SELECT BikeCode,Model FROM bike "; $result = mysqli_query($db, $sql) or die("Error: Failed to query database. ({$sql})"); while($row = mysqli_fetch_array($result)) { echo $row['BikeCode'] . " " . $row['Model']; echo "<br />"; } } ?> Edited November 27, 2012 by Krux20 Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395631 Share on other sites More sharing options...
PFMaBiSmAd Posted November 27, 2012 Share Posted November 27, 2012 The error message you are getting now cannot possibly be the same one at the start of this thread, because you are using the WRONG variable name as the first parameter to the mysqli_query() statement. $db ISN'T the connection link. You should be getting a php error message at the mysql_query() statement. Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395646 Share on other sites More sharing options...
Krux20 Posted November 27, 2012 Author Share Posted November 27, 2012 Thanks! you were right Quote Link to comment https://forums.phpfreaks.com/topic/271254-shopping-cart-sessions/#findComment-1395668 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.