INSERT, DELETE, EDIT, UPDATE OPERATION USING PHP AND MY SQL

This is one of the simple php code to insert the data and delete edit update and view operation using php and mysql the below images shows the view of the data you may delete and edit the data using the links in the table.

The particular link will delete or edit the particular data using id. You can insert the data in the same page using the above form. It directly insert  your data and it will be listed in the below table.

I given the full source code here to for simple insert, view, edit, update functionality using php and mysql. Here i am not used any ajax concepts.

This is one of the simple idea but you can implement projects using this as a base. The below example shows all operation about the data. download the snippet for free.


insert-delete-edit-update



Source Code

database:

create database sanwebcorner;

Table:

CREATE TABLE IF NOT EXISTS emp (

  `EmpID` int(11) NOT NULL AUTO_INCREMENT,
  `EmpName` varchar(150) NOT NULL,
  `Designation` varchar(150) NOT NULL,
  `Age` varchar(150) NOT NULL,
  `Salary` year(4) NOT NULL,
  PRIMARY KEY (`EmpID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;


Dumping data for table:

INSERT INTO emp (`EmpID`, `EmpName`, `Designation`, `Age`, `Salary`) VALUES

(1, 'Sandilyan', 'webdesigner', '26', 20000),
(2, 'Rajkumar', 'contentdeveloper', '25', 15000),
(3, 'Naveen', 'Graphicdesigner', '28', 12000);



db.php:

<?php
$conn=mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db("sanwebcorner",$conn);
?>


sanstyle.css

@charset "utf-8";
/* CSS Document */

body
{
margin:0px;
}

.head
{
background-color:#80B000;
height:90px;
}

.frm
{
height:370px;
width:700px;
margin-left:auto;
margin-right:auto;
margin-top:100px;
}


.san
{
width:800px;
margin-left:auto;
margin-right:auto;
border:1px solid #E3E3E3;
border-radius:10px;
height:auto;
line-height:20px;
padding-left:60px;
min-height:80px;
padding-top:10px;
padding-bottom:40px;
line-height:32px;
font-size:18px;
}

.txt
{
height:25px;
width:250px;
}

.sub
{
margin-left:auto;
margin-right:auto;
width:100px;
height:35px;
margin-left:250px;
}

.foot
{
margin-top:90px;
background-color:#80B000;
height:90px;
color:#FFFFFF;
}



index.php


<!--http://www.sanwebcorner.com/-->

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Insert delete edit update operation using php mysql</title>
<link rel="stylesheet" type="text/css" href="css/sanstyle.css">
</head>
<body>
<div class="head"></div>
<div class="frm">
<div class="san">
<h3 style="text-decoration:underline;">INSERT DELETE EDIT UPDATE OPERATION USING PHP MYSQL</h3>
<form name="insert" method="post" action="" >
<label>Employee Name :</label>
<input type="text" name="name" style="margin-left:75px;"  class="txt" ><br><br>
<label>Designation :</label>
<input type="text" name="desig"  style="margin-left:108px;" class="txt"><br><br>
<label>Employee Age :</label>
<input type="text" name="age"  style="margin-left:85px;" class="txt"><br><br>
<label>Salary :</label>
<input type="text" name="salary"  style="margin-left:142px;" class="txt"><br><br>
<input type="submit" name="submit" value="INSERT" class="sub">    
  <?php
if (isset($_POST['submit']))
{   
include 'db.php';
$name=$_POST['name'] ;
$desig= $_POST['desig'] ;
$age=$_POST['age'] ;
$salary=$_POST['salary'] ;
mysql_query("INSERT INTO emp(EmpName,Designation,Age,Salary) 
VALUES ('$name','$desig','$age','$salary')"); 
echo"<script> alert('Insert Success')</script>";
       }
?>  
      
</form>
<br><br>
<table border="1" cellpadding="10" width="750">
<?php
include("db.php");
$result=mysql_query("SELECT * FROM emp");
while($test = mysql_fetch_array($result))
{
$id = $test['EmpID'];
echo "<tr align='center'>";
echo"<td><font color='black'>" .$test['EmpID']."</font></td>";
echo"<td><font color='black'>" .$test['EmpName']."</font></td>";
echo"<td><font color='black'>". $test['Designation']. "</font></td>";
echo"<td><font color='black'>". $test['Age']. "</font></td>";
echo"<td><font color='black'>". $test['Salary']. "</font></td>";
echo"<td> <a href ='view.php?EmpID=$id'>Edit</a>";
echo"<td> <a href ='del.php?EmpID=$id'><center>Delete</center></a>";
echo "</tr>";
}
mysql_close($conn);
?>
</table>
</div>
</div>
</body>
</html>

<!--http://www.sanwebcorner.com/-->



view.php

<?php
require("db.php");
$id =$_REQUEST['EmpID'];
$result = mysql_query("SELECT * FROM emp WHERE EmpID  = '$id'");
$test = mysql_fetch_array($result);
if (!$result) 
{
die("Error: Data not found..");
}
$EmpName=$test['EmpName'] ;
$Designation= $test['Designation'] ;
$Age=$test['Age'] ;
$Salary=$test['Salary'] ;

if(isset($_POST['save']))
{
$name_save = $_POST['name'];
$desig_save = $_POST['desig'];
$age_save = $_POST['age'];
$salary_save = $_POST['salary'];

mysql_query("UPDATE emp SET EmpName ='$name_save', Designation ='$desig_save',
Age ='$age_save',Salary ='$salary_save' WHERE Empid = '$id'")
or die(mysql_error()); 
echo "<script>alert('Saved!')</script>";
header("Location: index.php");
}
mysql_close($conn);
?>


<!--http://www.sanwebtutorials.blogspot.in/-->


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Insert delete edit update operation using php mysql</title>
<link rel="stylesheet" type="text/css" href="css/sanstyle.css">
</head>
<body>
<div class="head"></div>
<div class="frm">
<div class="san">
<h3 style="text-decoration:underline;">INSERT DELETE EDIT UPDATE OPERATION USING PHP MYSQL</h3>
<form name="insert" method="post" action="" >
<label>Employee Name :</label>
<input type="text" name="name" style="margin-left:75px;"  class="txt"  value="<?php echo $EmpName ?>"><br><br>
<label>Designation :</label>
<input type="text" name="desig"  style="margin-left:108px;" class="txt" value="<?php echo $Designation ?>"><br><br>
<label>Employee Age :</label>
<input type="text" name="age"  style="margin-left:85px;" class="txt" value="<?php echo $Age ?>"><br><br>
<label>Salary :</label>
<input type="text" name="salary"  style="margin-left:142px;" class="txt" value="<?php echo $Salary ?>"><br><br>
<input type="submit" name="save" value="UPDATE" class="sub">
</form>
</div>
</div>
<div class="foot"><br>http://www.sanwebtutorials.blogspot.in</div>
</body>
</html>

<!--http://www.sanwebcorner.com/-->




del.php

<?php
  include("db.php");  
$id =$_REQUEST['EmpID'];
// sending query
mysql_query("DELETE FROM emp WHERE EmpID = '$id'")
or die(mysql_error());  
header("Location: index.php");
?>




Download Source Code Here

Post a Comment

0 Comments