How to Create Update Products in PHP

Hello guys, in this tutorial i will teach you how to create Update Form in every Products in PHP. This simple tutorial updates the products and the quantity of every data of product. Here are the sample script that i will show you.

Sample Code

PHP Script For the proucts list
  1. <?php
  2.         include "connection.php";
  3.         $q="select * from tblprod order by prod_id asc";
  4.         $result = mysql_query($q);
  5.         while($rs=mysql_fetch_array($result)){
  6.         $prid=$rs['prod_id'];
  7.         $prname=$rs['prod_name'];
  8.         $categ=$rs['prod_ctry'];
  9.         $qty=$rs['prod_qty'];
  10.         echo "<tr class='tb1'>";
  11.         echo "<td>".$prid."</td>";
  12.         echo "<td>".$prname."</td>";
  13.         echo "<td>".$categ."</td>";
  14.         echo "<td>".$qty."</td>";
  15.         echo "<td> <a href='edit.php?id=$prid' id='popedit'>Edit</a></td>";
  16.         echo "</tr>";
  17. }
  18. ?>
Result Process Script for the updating the Products
  1. <?php
  2.         include 'connection.php';
  3.         function edit_product($id,$prid,$prname,$prctry,$prqty){
  4.         $q="update tblprod set prod_id='$prid', prod_name='$prname',prod_ctry='$prctry',prod_qty='$prqty' where prod_id='$prid'";
  5.         if(mysql_query($q)){
  6.         echo "<script language='javascript'>
  7.                 alert('PRODUCT UPDATED SUCCESSFULLY');
  8.                 window.location = 'index.php';
  9.                 </script>";
  10. }
  11.         else{
  12.         echo "<script language='javascript'>
  13.                 alert('Error While Updating Product');
  14.                 window.location = 'edit.php';
  15.                 </script>";    
  16.                 }
  17. }
  18.  
  19.         if(isset($_POST['eprodid'],$_POST['eprodname'],$_POST['ecategory'],$_POST['equantity'])){
  20.         $prid=$_POST['eprodid'];
  21.         $prname=$_POST['eprodname'];
  22.         $prctry=$_POST['ecategory'];
  23.         $prqty=$_POST['equantity'];
  24.         edit_product('',$prid,$prname,$prctry,$prqty);
  25. }
  26. ?>
I hope that you learn in this tutorial. For more source code and tutorials just visit www.sourcecodester.com and don't forget to Like and Share. Thanks.
Tags

Add new comment