phptrouble1 Posted September 8, 2014 Share Posted September 8, 2014 (edited) Hello this may sound a little confusing. I recently made a fake website using this phpacademy tutorial I found. Anyways it has an quantity stock what I mean by this islet's I have 3 copies of a video game an a person set they quantity for 2 of the 3 copies and proceed to the checkout through paypal payment successful okay.My question is: How can I go about sending the quantity stock back to the database to subtract 2 from 3 to be left with 1 copy of the game in the quantity section after a successful purchase?Code is below in case I confused someone ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////// Maximumize / calling the quantity from Database ///////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(isset($_GET['add'])){ $quantity = mysqli_query($db_conx,'SELECT id, quantity FROM products WHERE id='.mysqli_real_escape_string($db_conx,(int)$_GET['add'])); while ($row = mysqli_fetch_assoc($quantity)){ if ($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); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////// Calling all products from Database ///////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function products($db_conx){ $get = mysqli_query($db_conx,"SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC"); if (mysqli_num_rows($get) ==0){ echo "There are no products to display"; } else { while ($row = mysqli_fetch_assoc ($get)){ echo '<p>'.$row ['name']. '<br/> <br/>' .$row ['description']. '<br/> <br/>' .'$'.number_format($row['price'], 2).' <a href="cart.php?add='.$row ['id'].'">Add</a></p>'; } } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////// Paypal Checkout ///////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function paypal_items($db_conx){ $num = 0; foreach($_SESSION as $name => $value){ if ($value != 0){ if(substr($name, 0, 5)=='cart_'){ $id = substr($name, 5, strlen($name) -5); $row = mysqli_query($db_conx,'SELECT id, name, price, shipping FROM products WHERE id ='.mysqli_real_escape_string($db_conx,(int)$id)); while ($query = mysqli_fetch_assoc($row)){ $num++; echo '<input type="hidden" name="item_number_'.$num.'" value="'.$id.'">'; echo '<input type="hidden" name="item_name_'.$num.'" value=" '.$query['name'].' ">'; echo '<input type="hidden" name="amount_'.$num.'" value=" '.$query['price'].' ">'; echo '<input type="hidden" name="shipping_'.$num.'" value=" '.$query['shipping'].' ">'; echo '<input type="hidden" name="quantity_'.$num.'" value=" '.$value.' ">'; } } } } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////// Shopping Cart View ///////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function cart($db_conx) { foreach($_SESSION as $name => $value) { if ($value > 0) { if(substr($name,0 ,5) . 'cart_'){ $id = substr($name, 5, (strlen($name) -5)); $row = mysqli_query($db_conx,'SELECT id, name, price FROM products WHERE id=' .mysqli_real_escape_string ($db_conx,(int)$id)); while ($query = mysqli_fetch_assoc($row)){ $sub = $query['price']*$value; echo $query['name'].' x ' .$value. ' @ $' .number_format ($query['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/> <br/> '; } } $total += $sub + $shipping; } } if ($total ==0) { echo "Your cart is empty."; } else { echo 'Total: $' .number_format($total, 2); ?> <form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="upload" value="1"> <input type="hidden" name="business" value="youremail@outlook.com"> <?php paypal_items($db_conx); ?> <input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="amount" value="<?php echo $total ;?>"> <input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but03.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!"> </form> <?php } } ?> Edited September 10, 2014 by mac_gyver code tags please Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 8, 2014 Share Posted September 8, 2014 the short answer is to treat your inventory as a deposit/debit account, where you have a row for each transaction. each row contains all the who, what, when, where, and why information about the transaction. when inventory is added (including the initial quantity), you insert a row with a positive quantity (deposit). when inventory is added to a 'cart' and the person go to the checkout stage, you would you add a row to the database table for each item in the cart, listing a negative quantity (debit), but with a status value that indicates is has been 'ordered'. when the item is actually pulled and shipped, the status for each item would be changed to 'shipped'. to get the current quantity of any item(s), you would simply run a query that sums up the positive and negative quantities for each item id/item number. Quote Link to comment Share on other sites More sharing options...
phptrouble1 Posted September 9, 2014 Author Share Posted September 9, 2014 Is it possible that I email you to get a better understanding or lesson on how to go about creating the negative quantities and such I never could pull off all adding additional stuff to paypal checkout I do pay for private lesson or help. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 10, 2014 Share Posted September 10, 2014 in actuality, once you have the necessary database tables set up, the only thing you need to do differently in any cart code to accomplish what i suggested is to change the queries that operate on the data. the following is a little different from what i described above (which is why you should work out the details yourself, rather than follow along with what someone quickly writes out in a post somewhere on the web.) instead of having a query that updates a row to subtract the quantity in your products table, you would instead insert a new row into a different table (such as a cart/order_details table), with the item id and the quantity that was ordered (if you store the cart in a database table, instead of a session, this step isn't needed since the item id/quantity would already be in a database table.) any query that needs to get the current quantity, instead of reading the row(s) from your products table, would take the quantity on hand and subtract the SUM() of the quantity from this new table. if you do an advanced search on the forum for 'cart' and my user name, you should find a lot previously written information about this. in fact, see the second half of the post at the following link -- http://forums.phpfreaks.com/topic/286051-php-shopping-cart-and-quantity-help/?hl=%2Bcart&do=findComment&comment=1468275 and i added the forum's bbcode tags around your posted code above, as that may have been a reason no one bothered to look at the code to offer any specific help with it. 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.