php edit delete records

      Comments Off on php edit delete records

php edit delete records

php edit delete records

php edit delete records example explains how you can do basic operations like update records, edit records or delete records in php programming language. This php tutorial is for beginners / students who want to learn this functionality first time. Most of the students who has just started learning php are facing many problems in edit/delete operation. Here i have prepare a complete tutorial which focuses on Edit and Delete operations in PHP with easy understanding.

Did you know? How to Delete multiple rows using php?

Note: This article is prepared for beginners only, This version/coding pattern can not be used directly while developing live website.

Database

CREATE TABLE IF NOT EXISTS `student` (
`sid` int(11) NOT NULL,
  `sname` varchar(20) NOT NULL,
  `smobile` varchar(10) NOT NULL,
  `sgender` varchar(1) NOT NULL,
  `scity` varchar(20) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `student`
--

INSERT INTO `student` (`sid`, `sname`, `smobile`, `sgender`, `scity`) VALUES
(1, 'Ram', '1234567890', 'M', 'Vadodara'),
(2, 'Sita', '2323232', 'F', 'Mumbai');

 

 

FileName : index.php

<?php session_start(); 
//php edit delete records ?>

<html>
	<head>
		<title>	php edit delete records</title>
	</head>
	<body bgcolor="#E0E0E0">
	
	Welcome to php edit delete records Tutorial
	
<?php
//============= Variables for Database ===================
$hostname = "localhost";
$username = "root";
$password = "";
$database = "mydb";
//========================================================

//Connection...
$link = mysql_connect($hostname, $username, $password);

//Set Database
mysql_select_db($database,$link);

//Check Page1 username and password with our user_master table.
$query = "select * from student";
$result = mysql_query($query);

echo "<br><br>";
echo "<table border=\"1\">";
echo "<tr><td>sid</td><td>sname</td><td>smobile</td><td>sgender</td><td>scity</td><td>Edit</td><td>Delete</td></tr>";

while($row=mysql_fetch_array($result))
{
	
	$sid=$row['sid'];
	$sname=$row['sname'];
	$smobile=$row['smobile'];
	
	if($row['sgender']=="M")
		$sgender="Male";
	else
		$sgender="Female";
	
	$scity=$row['scity'];
	
	echo "<tr><td>$sid</td><td>$sname</td><td>$smobile</td><td>$sgender</td><td>$scity</td><td> <a href=\"edit.php?id=$sid\"> Edit </a></td> <td> <a href=\"delete.php?id=$sid\" onclick=\"return confirm('Are you sure?');\"> Delete </a></td></tr>";
}
echo "</table>";
?>

					<?php
						if(isset($_SESSION['msg']))
						{
							//echo ""$_SESSION['msg'];
							echo "<script language=\"JavaScript\">alert(\"".$_SESSION["msg"]."\");</script>";
							unset($_SESSION['msg']);
						}
					?>
	
	
	
	</body>
</html>

 

FileName : edit.php

<?php session_start();
//php edit delete records
 ?>

<html>
	<head>
		<title>	Profile</title>
	</head>
	<body bgcolor="#E0E0E0">
	
	Welcome to PHP Edit Delete Tutorial
	
	<br><br>
	<h2> Edit Operation </h2>
<?php
//============= Variables for Database ===================
$hostname = "localhost";
$username = "root";
$password = "";
$database = "mydb";
//========================================================

//Connection...
$link = mysql_connect($hostname, $username, $password);

//Set Database
mysql_select_db($database,$link);

//Check Page1 username and password with our user_master table.
$query = "select * from student where sid=".$_GET["id"];
$result = mysql_query($query);

echo "<br>";
echo "<form method=\"post\" action=\"edit2.php\">";
echo "<table border=\"1\">";
//echo "<tr><td>sid</td><td>sname</td><td>smobile</td><td>sgender</td><td>scity</td><td>Edit</td><td>Delete</td></tr>";

while($row=mysql_fetch_array($result))
{
	
	$sid=$row['sid'];
	$sname=$row['sname'];
	$smobile=$row['smobile'];
	
	if($row['sgender']=="M")
		$sgender="Male";
	else
		$sgender="Female";
	
	$scity=$row['scity'];
	
	echo "<tr>";
	
	echo "<tr><td>ID</td><td><input type=\"text\" name=\"sid\" readonly value=\"$sid\"></td></tr>";
	echo "<tr><td>Name</td><td><input type=\"text\" name=\"sname\" value=\"$sname\"></td></tr>";
	echo "<tr><td>Mobile</td><td><input type=\"text\" name=\"smobile\" value=\"$smobile\"></td></tr>";
	
	echo "<tr><td>Gender</td>";
	
	if($sgender=="Male")
	{
		echo "<td> <input type=\"radio\" name=\"sgender\" value=\"M\" checked>Male <br>
<input type=\"radio\" name=\"sgender\" value=\"F\">Female</td></tr>";
	}
	else
	{
		echo "<td> <input type=\"radio\" name=\"sgender\" value=\"M\">	Male <br>
<input type=\"radio\" name=\"sgender\" value=\"F\" checked>Female</td></tr>";
	}
	
	
	echo "<tr><td>City</td><td><input type=\"text\" name=\"scity\" value=\"$scity\"></td></tr>";
	
}
echo "</table>";
echo "<br>";
echo "<input type=\"submit\" value=\"Update\">";
echo "</form>";

?>
	
	
	</body>
</html>

 

FileName : edit2.php

<?php session_start(); 

//============= Variables for Database ===================
$hostname = "localhost";
$username = "root";
$password = "";
$database = "mydb";
//========================================================

//Connection...
$link = mysql_connect($hostname, $username, $password);

//Set Database
mysql_select_db($database,$link);

$sid=$_POST['sid'];
$sname=$_POST['sname'];
$smobile=$_POST['smobile'];
$sgender = $_POST['sgender'];
$scity=$_POST['scity'];
	
//Check Page1 username and password with our user_master table.
$query = "update student set sname='$sname',smobile='$smobile',sgender='$sgender',scity='$scity'  where sid=".$_POST["sid"];
$result = mysql_query($query);

$_SESSION["msg"]="Record Updated";
header('Location: index.php');
?>

 

FileName: delete.php

<?php session_start(); 

//php edit delete records


//============= Variables for Database ===================
$hostname = "localhost";
$username = "root";
$password = "";
$database = "mydb";
//========================================================

//Connection...
$link = mysql_connect($hostname, $username, $password);

//Set Database
mysql_select_db($database,$link);

//Check Page1 username and password with our user_master table.
$query = "delete from student where sid=".$_GET["id"];
$result = mysql_query($query);

$_SESSION["msg"]="Record Deleted";
header('Location: index.php');
?>

 

Screenshots 1

php edit delete records

Screenshot 2

php edit delete records

Screenshot 3

php edit delete records

Screenshot 4

php edit delete records

Screenshot 5

php edit delete records

Screenshot 6

php edit delete records

Download Code + Database Backup (SQL)

[sociallocker]Download[/sociallocker]