Subtract Product From Database In Ecommerce Website

Problem Statement:

A user want to buy any product from different products which are available.
This scnerio will tell you that how it is bought by the user and after buying the product ,remaining quantity is updated in our database.
If the database does not have the desired quantity available it will prompt the user to purchase some other item or show message that he cannot purchase it.
The things you are required for this task are given below:

      Xampp or Wamp server.
      Download Xampp
      Download Wampp

      An editor to  write php and html script.

      A web browser to see your desired output.

      A database engine such as Mysql.

Description:

First of All you have to setup a database.
Create a database with the name test.
Create a table in this database with the name tbldata.
In this database you are required three fields.

      product_id

      productName

      productQty.

Now, write down insert query to insert some products into your database.

INSERT INTO tbldata (product_id, productName, productQty)
VALUES (‘’,’Keyboard’,55), VALUES (‘’,’Mouse’,22), VALUES (‘’,’Monitor’,45)

Then you need to open your text editor to write the following code accordingly.

Subtract Product Example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Product Qty Subtractor</title>

</head>

<pre>

<body bgcolor="yellow">

<i><h1>Subtrating product quantity from database in ecommerce website:</h1></i>

<center>

<?php

$conn=mysqli_connect('localhost','root','','test');

?>

<form method="post" action="">

<b>product name:  </b>

<?php

$query="select productName from tblData";

$rs=mysqli_query($conn,$query);

echo "<select name='proName'>";

while($row=mysqli_fetch_array($rs))

{

echo "<option value='".$row["productName"]."'>".$row["productName"]."</option>";

}

echo "</select>";

?>
<br>

<b>Product Quantity:</b>

<input type="text" name="prodQty">

<br>

<input type="submit" value="submit" name="purchase">

</form>

</center>

<?php

if(isset($_POST[purchase]))

{

$purchaseQty=$_POST['prodQty'];

$proName=$_POST['proName'];

$query="select productQty from tbldata where productName='".$proName."'";

$result=mysqli_query($conn,$query);

$row=mysqli_fetch_array($result,MYSQLI_NUM);

echo $row[0];

 $rowcount=mysqli_num_rows($result);

if($rowcount>0)

{
  $saveQty=0;

   $answer=0;

     $saveQty=(int)$row[0];

         if($purchaseQty>$saveQty)

          {

                 echo "<script>alert('sorry it has only quantity of ".$saveQty."')</script>";

          }

           else
         {

                 echo "<script>alert('Thanks for purchasing this product')</script>";

                 $answer=(int)($saveQty-$purchaseQty);

               mysqli_query($conn,"update tbldata set productQty='".$answer."' where productName='".$proName."'");

          }

         }

          else

            {

                 echo 'Sorry it has no products available.';

             }

     mysqli_close($conn);                    

}

?>

</body>
</html>



In this code snippet, first there is a  combobox which is made from the HTML elements.In this combobox all the products are coming from database.There is another input text field for product Qty which is entered by the user.
Now when the user enter the quantity, first it will check that if this quantity is available in the database for the required product.If is is available it will prompt the user that:
Thanks for purchasing.
In the second case if the desired quantity that the user enters is not available in the database then it will inform the user that how much quantity is there to purchase.
Last point is that, after user click on purchase button the system will update the remaing quantity in the database.