Ex No: 14 Write a PHP Script to perfrom Insert and Delete Operation using Form Processing
Aim: To perform Insert and Delete Operation using Form Processing
Algorithm
Step1: Create a new Database in MySQL "cartdb".
Step2: Create a new Table "product_table" with the following columns.
   A) "prod_id" as Primary key, data type varchar(5).
   B) "prod_name" data type varchar(50).
   C) "prod_desc" data type text.
   D) "prod_price" data type float.
   E) "prod_image" data type varchar(10).
Step3: Create "index.php" and "dbcon.php" file to access database and display product details.
Step4: Create "upload.php" file to insert new product details in the database and "images" folder to upload product image to the server.
Step5: Create "del.php" to delete a product from the database.

index.php


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Form Processing</title>
</head>
<html>
 <body> 
 <?php
  include("dbcon.php");
  $obj = new db_con();
  $tbody = $obj->get_prod();
 ?>
 <h1>Form Processing Shopping Cart Data</h1>
 <form action="upload.php" method="POST" enctype="multipart/form-data">
  <table>
   <thead>
    <tr>
     <th>Product ID</th>
     <th>Product Name</th>
     <th>Product Description</th>
     <th>Product Price</th>
     <th>Product Image</th>
   </tr>
  </thead>
  <tbody>
   <?php
    echo $tbody;
   ?>
  </tbody>
  <tfoot>
   <tr>
    <td>
     <input type="text" name="pid" required>
    </td>
    <td>
     <input type="text" name="pname" required>
    </td>
    <td>
     <textarea name="pdesc" required>
     </textarea>
    </td>
    <td>
     <input type="text" name="pprice" required>
    </td>
    <td>
     <input type="file" name="pimg" required>
    </td>
   </tr>
  </tfoot>
 </table>  
 <input type="submit" value="Add Product" name="submit">
 </form>
 <hr>
 <h1>Delete Product</h1>
 <form action="del.php" method="POST">
  <table>
   <tr>
    <td>Product ID <input type="text" name="delpid"></td>
    <td><input type="submit" value="Delete Product" name="submit"></td>
   </tr>
  </table>
 </form>
</body>
</html>
                   

dbcon.php


 <?php
class db_con
{
 private $host;
 private $db;
 private $usr;
 private $pwd;
 private $con;
 
 function __construct()
 {	
  $this- >host = "localhost:3306";
  $this- >db = "cartdb";
  $this- >usr = "root";
  $this- >pwd = '';
  $this- >con = mysqli_connect($this- >host,$this- >usr,$this- >pwd,$this- >db);
  if(mysqli_connect_errno())
  {
   echo "Connection Error";
  }
 }
	
 function get_prod()
 {
  $qry = "SELECT prod_id, prod_name, prod_desc, prod_price, prod_image FROM product_table";
  $res = mysqli_query($this- >con,$qry) or die("Error in Query");
  $numrows = mysqli_num_rows($res);		
  if($numrows >=1)
  {
   $row="";
   while($val = mysqli_fetch_array($res))
   {
    $img = " <img src='images/$val[4]' width='60px' height='60px' >";
    $row = $row." <tr > <td >$val[0] </td > <td >$val[1] </td > <td >$val[2] </td > <td >Rs. $val[3] </td > <td >$img </td > </tr >";
   }
  }
  else
  {
   $row = " <tr > <th colspan='5' > <h1 >No Product in Database </h1 > </th > </tr >";
  }
  return $row;
 }
	
 function insert_prod($pid, $pname, $pdesc, $pprice, $pimg)
 {
  $qry="INSERT INTO product_table(prod_id, prod_name, prod_desc, prod_price, prod_image) VALUES ('$pid','$pname','$pdesc',$pprice,'$pimg')";
  $res = mysqli_query($this- >con,$qry) or die("Unable to Insert Product");	$numrows = mysqli_affected_rows($this- >con);
  if($numrows == 1)
  {
   return true;
  }
  else
  {
   return false;
  }
 }

 function del_prod($pid)
 {
  $qry = "DELETE FROM product_table WHERE prod_id = '$pid'";
  $delres = mysqli_query($this- >con,$qry) or die("Unable to Delete Product");		
  $numrows = mysqli_affected_rows($this- >con);
  if($numrows == 1)
  {
   return true;
  }
  else
  {
   return false;
  }
 }

}
? >
                  

upload.php


 <?php
 include("dbcon.php");
 $obj = new db_con();
 
 //Get user form data
 $pid = $_POST["pid"];
 $pname = $_POST["pname"];
 $pdesc = $_POST["pdesc"];
 $pprice = $_POST["pprice"];
 $imgname = $_FILES["pimg"]["name"];

 //Rename Image file name
 $tmp = explode(".",$imgname);
 $pimg = $pid.".".end($tmp);
 $target_file = "images/".$pimg;


 // Check image and insert Data
 if(isset($_POST["submit"])) 
 {
  $insertRes = $obj- >insert_prod($pid, $pname, $pdesc, $pprice, $pimg);
  $check = getimagesize($_FILES["pimg"]["tmp_name"]);
  if(($check !== false) && $insertRes) 
  {
   if (move_uploaded_file($_FILES["pimg"]["tmp_name"], $target_file)) 
   {
    header("Location: index.php");
   }
  }
  else
  {
   echo "Unable to Insert Product";
  }
 }
? >

                  

del.php


 <?php
 include("dbcon.php");
 $obj = new db_con();
 
 //Get user form data
 $pid = $_POST["delpid"];

 if(isset($_POST["submit"])) 
 {
  $delres = $obj- >del_prod($pid);
  if($delres) 
  {
   header("Location: index.php");
  } 
  else 
  {
   echo "Unable to Delete Product";
  }
 }
? >

                  
Result: Thus a webpage is designed using php to perform Array Processing.