How to Insert, Delete and Update Data in MySQL Database Using PHP? [With Source Code]
Are you are searching for tutorial about inserting, deleting and updating data in your MySQL database through PHP code?
If yes, then you are at right place. We will be talking about how to insert, update and delete data in MySQL database using PHP via HTML form.
This are very basic operation performed in any database related programs. So lets start.
Prerequisite:
You need to have any server application installed in your PC. It is must for executing any PHP script because PHP is server side scripting language.
I am using WAMP Server but you can use any server application software.
Steps
1) After installing WAMP, Start your WAMP Server
2) Open Localhost
3) Find phpMyAdmin and Click on it.
4) Create database stud_mgmt
5) Create table Student
CREATE TABLE IF NOT EXISTS `student` (
`rno` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`dept` varchar(50) NOT NULL,
`phone` bigint(20) NOT NULL,
`sem` int(11) NOT NULL,
PRIMARY KEY (`rno`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Read this article to learn how to create database in phpMyAdmin
6) Open your any text editing software - I am using Notepad, you can use Dreamweaver 8 or CC, and then define site and testing server for your Dreamweaver cc, Read this article for more information.
7) After completing this task, write the following piece of code and save it in .php extension.
<html>
<head>
<title>Data Management using form </title>
</head>
<body bgcolor="lightgray">
<form action = "<?php $PHP_SELF ?>" method = "POST">
<table border = "2" cellpadding = "5" bgcolor = "white">
<tr>
<td> Roll No: </td>
<td> <input type = "text" name = "rno">
</tr>
<tr>
<td> Student Name: </td>
<td> <input type = "text" name = "sname">
</tr>
<tr>
<td> Department: </td>
<td> <input type = "text" name = "dept">
</tr>
<tr>
<td> Phone: </td>
<td> <input type = "text" name = "ph">
</tr>
<tr>
<td> Semester: </td>
<td> <input type = "text" name = "sem">
</tr>
<tr>
<td colspan = "2" align = "center">
<input type = "submit" name = "Add" value = "Add">
<input type = "submit" name = "Remove" value = "Remove">
<input type = "submit" name = "Edit" value = "Edit">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
$conn = mysql_connect("localhost","root","");
if(isset($_POST['Add']))
{
if ($conn)
{
mysql_select_db("stud_mgmt", $conn);
$rno = $_POST['rno'];
$sn = $_POST['sname'];
$dt = $_POST['dept'];
$ph = $_POST['ph'];
$sem = $_POST['sem'];
$ins = "INSERT INTO student VALUES ($rno, '$sn', '$dt', $ph, $sem)";
if (mysql_query($ins,$conn))
{
echo "Record Added Successfully";
}
}
}
/* code to remove the record */
if(isset($_POST['Remove']))
{
$conn = mysql_connect("localhost","root","");
if ($conn)
{
mysql_select_db("stud_mgmt", $conn);
$rno = $_POST['rno'];
$rm = "DELETE FROM student WHERE rno = ".$rno;
if (mysql_query($rm,$conn))
{
echo "Record Removed Successfully";
}
}
}
/* Code to edit the record */
if(isset($_POST['Edit']))
{
$conn = mysql_connect("localhost","root","");
if ($conn)
{
mysql_select_db("stud_mgmt", $conn);
$rno = $_POST['rno'];
$sn = $_POST['sname'];
$dt = $_POST['dept'];
$ph = $_POST['ph'];
$sem = $_POST['sem'];
$upt = "UPDATE student Set name = '".$sn."',dept = '".$dt."', phone = ".$ph.",sem = ".$sem." WHERE rno = ".$rno;
if (mysql_query($upt,$conn))
{
echo "Record Edited Successfully";
}
}
}
?>
8) Open this .php file in any browser you want and Use the forms to add, view, update, and delete tasks.
If you have any confusion, you can watch the video.