Add, List, Edit, Delete Record in Database Using PHP

By | November 6, 2012

Add, List, 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)

Add record – Insert sql query
List 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 :  freeze_demo

TABLE NAME:  addd

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

Stylesheet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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
1
2
3
4
5
6
7
<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("freeze_demo",$query);
?>
add.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<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>

 

list.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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<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" value="<?php echo $query2['name']; ?>" /><br />
Age:<input type="text" name="age" value="<?php echo $query2['age']; ?>" /><br /><br />
<br />
<input type="submit" name="submit" value="update" />
</form>
<?php
}
?>
</body>
</html>

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<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>

 

65 thoughts on “Add, List, Edit, Delete Record in Database Using PHP

  1. ShaynE

    can you help me? i have a project in web development.. database using php..
    it is about billing..thanks

    Reply
  2. DEVI

    i tried the above delete.php.bt it is not working in my project.

    the code is:

    what is the error in it?
    if i have to use session?

    Reply
  3. sammy martin

    I know you must be a very busy person. But, I must ask if you are available to assit me with one php script for a project that I am working on. If so, please contact me as soon as possible as I am running out of time. Also, please let me know your charges.

    Reply
  4. Martijn

    Very useful, but for those who put this online as a active system, be aware that his is not save! you can delete records by just typing delete.php?id=…

    So you should have a script running that checks if someone is logged in, if so, execute the script, if not, return to login.php or something like that.

    Reply
  5. Paul Walker

    Never trust the user input! When the user enters a string with a ‘ in it, you’ll end up with a sqlinjection.

    THIS CODE IS NOT SAFE! DO NOT USE THIS CODE ON YOUR OWN SITE!

    Reply
    1. prasad k Post author

      I just given the basic code. We have to use Client side validation to ensure security.

      Reply
  6. wale

    i have web project,but my rigster page only rigster one data ,what is the problem?

    Reply
  7. Vinod

    I am displaying data from my db, now i want to put a check box for deletion. If the user checks and submits it than the del.php mus process it. so what data mus be sent to del.php so that it identifies the same row. I am not sure if there is anything called as rowid. I am very new to this. Please help me out.

    Reply
    1. prasad k Post author

      use checkbox value as user’s primary key(id) and using that id value perform delete operation

      Reply
    1. prasad k Post author

      DATABASE NAME : freeze_demo
      TABLE NAME: addd
      FIELDS: id(primary key with auto increment), name, age

      Reply
  8. sanghamitra

    when i am running the list.php it is showing me warning as follows

    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\freeze_demo\list.php on line 13

    Username Age

    can plz help me to check this error

    Reply
      1. yan

        same here, error
        Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\freeze_demo\list.php on line 13

        Reply
  9. Muhammad Zubair

    Dear Sir prasad k,

    Thank you for this nice simple project, I learned much much from the simple code, i did never foundthis kind of smart effecient coding in my 10years php experience. thank you and only God can reward you for this nice cooperation write much more your teaching way is x-c-lent by

    Reply
  10. Ajoo

    I am getting the same message as Sanghamitra above got. Namely

    “Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\xampp\dataEntry\list.php on line 8
    Username Age”

    So how can this be overcome?
    Thanks

    Reply
  11. Ajoo

    Dear Prasad,

    A very nice tutorial. I have been trying something similar where i have been trying to get the add and edit functionality into one form. Thus the add.php will have both add and edit buttons and when they are clicked the code for their processing should also be in the same add.php file. Can someone show how this may be achieved?

    Thanks loads

    Reply
  12. Nelson

    Thanks A million the edit , add, list and Delete code in PHP very well explained

    if you can help me in printing data coming from data page pagewise using php and mysql would be a good help

    Reply
  13. preeti

    when i am running the list.php it is showing me warning as follows

    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\freeze_demo\list.php on line 7

    Username Age

    Reply
  14. Raj

    hello,
    can we perform edit and delete operation on same page means without redirect to another new page. for ex. when I press edit the lesting page reloads and shoes the value within the textbox. this is necessary because if we make quite large project which contains 40-50 pages..then we have to make another 100 page for edition and deletion..plz help.

    Reply
  15. dhivya

    when i am running the list.php it is showing me warning as follows

    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\freeze_demo\list.php on line 7

    Username Age

    Reply
  16. sankar

    when i am running the list.php it is showing me warning as follows

    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\practies\list.php on line 13
    Name Age

    Reply
  17. Poojani Pathirana

    Very useful tutorial for the beginers.thank you so much….

    Reply
  18. venkatesh lohar

    thank you,
    nice explanation. this is more useful for beginners.

    Reply
  19. Tesloach Lul

    Thank you very much, you have done a great job. Stay bless

    Reply
  20. Saeed

    Hi Prasad,

    Thank you very much for your time you have spend in creating this form.
    It really help me in creating my first php/mysql form.

    Much appreciated !

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

colleagueship-septuor