X
    Categories: PHP

How to insert, edit, delete and view using PHP and Mysql?

Insert, View, Edit, Delete Record in Database Using PHP is a very simple task given to php newbie to check about their knowledge in php. This add, list, edit and delete record operation uses all basic SQL queries (i.e., insert, select, update, delete)

Insert record – Insert sql query
View record – Select sql query
Edit record – update sql query
Delete record – delete sql query

There are four php files are used here to do this operations.

config.php
add.php
list.php
edit.php
delete.php

Database

——————————————————————————————————————————–

DATABASE NAME :  17_demo

TABLE NAME:  addd

FIELDS:  id(primary key with auto increment), name, age

——————————————————————————————————————————–Stylesheet

<style type="text/css">
td
{
padding:5px;
border:1px solid #ccc;

}
</style>

PHP Code to Add Edit List Delete Record

Here is the simple php code for add, list, edit and delete record

config.php

<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("17_demo",$query);
?>

insert.php

<html>
<body>
<?php
include('config.php');
if(isset($_POST['submit']))
{
$name=mysql_real_escape_string($_POST['name']);
$age=mysql_real_escape_string($_POST['age']);
$query1=mysql_query("insert into addd values('','$name','$age')");
echo "insert into addd values('','$name','$age')";
if($query1)
{
header("location:list.php");
}
}
?>
<fieldset style="width:300px;">
<form method="post" action="">
Name: <input type="text" name="name"><br>
Age: <input type="text" name="age"><br>
<br>
<input type="submit" name="submit">
</form>
</fieldset>
</body>
</html>

  view.php

In list record page, for each records there is a edit and delete option. Edit option invokes edit.php page and delete option invokes delete.php

<html>
<body>
<?php
include('config.php');
$query1=mysql_query("select id, name, age from addd");
echo "<table><tr><td>Name</td><td>Age</td><td></td><td></td>";
while($query2=mysql_fetch_array($query1))
{
echo "<tr><td>".$query2['name']."</td>";
echo "<td>".$query2['age']."</td>";
echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>";
echo "<td><a href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
}
?>
</ol>
</table>
</body>
</html>

 

edit.php

When edit option in list page is clicked, we will be redirected to edit.php and by using  id value (primary key) values will be fetched from database.

<html>
<body>
<?php
include('config.php');
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$age=$_POST['age'];
$query3=mysql_query("update addd set name='$name', age='$age' where id='$id'");
if($query3)
{
header('location:list.php');
}
}
$query1=mysql_query("select * from addd where id='$id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="">
Name:<input type="text" name="name" >
 

delete.php

When delete (‘X’) option is clicked in list page, particular record will be deleted by using id values and redirected to list.php
<html>
<body>
<?php
include('config.php');
if(isset($_GET['id']))
{
$id=$_GET['id'];
$query1=mysql_query("delete from addd where id='$id'");
if($query1)
{
header('location:list.php');
}
}
?>
</body>
</html>

 

Thanks!. IF you like this article please don't forget to comment and share.

 

Marimuthu: