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 is let'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
}
}
?>