glo92 Posted May 15, 2015 Share Posted May 15, 2015 Hi, I was wondering if anyone can help me save the shopping cart to a database? ive looked all online but havent found anything. many thanks cart.php Quote Link to comment https://forums.phpfreaks.com/topic/296340-php-shopping-cart-to-database/ Share on other sites More sharing options...
CroNiX Posted May 15, 2015 Share Posted May 15, 2015 You'd have better luck if you posted the code in your post rather than having to download it. Many won't do that. Quote Link to comment https://forums.phpfreaks.com/topic/296340-php-shopping-cart-to-database/#findComment-1511985 Share on other sites More sharing options...
glo92 Posted May 15, 2015 Author Share Posted May 15, 2015 <?php session_start(); $page = 'ordering.php'; mysql_connect('localhost','root','') or die(mysql_error()); mysql_select_db ('cart') or die (mysql_error()); if (isset($_GET['add'])) { $quantity = mysql_query('SELECT id, quantity FROM dishes WHERE id='.mysql_real_escape_string((int)$_GET['add'])); while ($quantity_row = mysql_fetch_assoc($quantity)){ if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]){ $_SESSION["cart_".(int)$_GET['add']]+='1'; } } header('Location: '.$page) ; } if (isset($_GET['remove'])) { $_SESSION['cart_'.(int)$_GET ['remove']]--; header('Location: '.$page) ; } if (isset($_GET['delete'])) { $_SESSION['cart_'.(int)$_GET ['delete']]='0'; header('Location: '.$page) ; } function dishes(){ $get = mysql_query('SELECT id, name, description, price FROM dishes WHERE quantity > 0 ORDER BY id DESC'); if (mysql_num_rows($get)==0) { echo "There are no dishes to display!"; } else { while ($get_row = mysql_fetch_assoc($get)) { echo '<p>'.$get_row['name'].'<br />'.$get_row['description'].'<br />€'.number_format($get_row['price'], 2).' <a href="cart.php?add='.$get_row['id'].'"> Add</a></p>'; } } } function cart() { $total = 0; foreach($_SESSION as $name => $value) { if ($value>0) { if (substr ($name, 0, 5)=='cart_'){ $id = substr($name, 5, strlen ($name)-5); $get = mysql_query('SELECT id, name, price FROM dishes WHERE id='.mysql_real_escape_string((int)$id)) ; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price']*$value; echo $get_row['name'].' x '.$value.' @ €'.number_format($get_row['price'], 2). ' = €'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br />'; } } $total += $sub; } } if ($total == 0) { echo "no items."; } else { echo 'Total: €'.number_format($total, 2).'</p>'; ?> <html> <p> <form action='viewcart.php' method='POST'> <input type='submit' name='view' value='Confirm'> </p> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/296340-php-shopping-cart-to-database/#findComment-1511990 Share on other sites More sharing options...
CroNiX Posted May 15, 2015 Share Posted May 15, 2015 There's not really enough info to answer the question. All the code above shows is incrementing a counter in $_SESSION. It doesn't show anything about what's in the cart. What data from the cart are you trying to save and what does it look like? What's the schema for the db that you want to add the cart data into? Quote Link to comment https://forums.phpfreaks.com/topic/296340-php-shopping-cart-to-database/#findComment-1511991 Share on other sites More sharing options...
glo92 Posted May 15, 2015 Author Share Posted May 15, 2015 Im trying to save an ordering form (for a mock restaurant) to the admin db after the user adds the items to the cart and proceeds to the pay function which already directs them to paypal screen. So basically, im trying to save the dishes the user selects and the price/qty to the database, along with an order id. In the database i have a table called dishes with (Id,name,Description,Price and Quantity). Many Thanks for the reply. This is the html file to display the dishes and cart. <div class="callout"> <aside class="sidebar"> <br /> <fieldset> <?php cart(); ?> </fieldset> </div> <br /> <?php dishes (); ?> </body> <?php include 'footer.html'; ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/296340-php-shopping-cart-to-database/#findComment-1511998 Share on other sites More sharing options...
mac_gyver Posted May 16, 2015 Share Posted May 16, 2015 So basically, im trying to save the dishes the user selects and the price/qty to the database, along with an order id. we actually know what you are trying to do, what we need to know is where you are stuck at when you tried to do it, because the purpose of programming help forums are not to write complete working code for you or to think out the details of each step that you need to do, we are here to help when you get stuck on something, after you have made an attempt at doing it. have you defined the user steps for the confirm-order process? what information do you have and what will be displayed at each step, and what new information do you need from each step? seems that if the visitor is viewing the contents of the cart and are ready to finalize the order, wouldn't the next step be to collect any new customer information or confirm existing customer information, such as ship/deliver address (it could be different for each order), billing name/address (if they use a different payment method, this can be different for each order), ... then create a record in an 'orders' table for this customer on the date and time in question that would assign an order_id, that you would then use to relate and store the contents of the cart in an 'order_items' table? once you have done this, you would have a 'pay' form/button that would submit/take them to the paypal site for the actual payment process. the code for the particular step of inserting the data into the 'order_items' table, would in its simplest form, be to just loop over the contents of the cart, form and run the INSERT query with the order_id, the dish id, quantity, and if the price can vary, the price at the time of the order. for bonus points, you can form and run one efficient multi-value insert query, rather than running a query in a loop. the code you have for your session based cart needs to be simplified. you should create a $_SESSION['cart'] variable that is an array of the cart contents - $_SESSION['cart'][dish id] = quantity. this will eliminate all the substr() statements in the code. also, if the quantity of an item in the cart is/reaches zero, you should remove it from the cart. since the value stored in the cart is the quantity, you can just use php's array_filter() function to remove empty items from the cart. Quote Link to comment https://forums.phpfreaks.com/topic/296340-php-shopping-cart-to-database/#findComment-1512032 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.