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

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>

 

About prasad k

Prasad Kuppusamy has written 81 post in this blog.

i m php developer, web designer.

57 Responses to Add, List, Edit, Delete Record in Database Using PHP

  1. ShaynE says:

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

  2. lavanya says:

    nice it is very useful for beginners.

  3. DX Baidan says:

    where is the download link????

  4. raman kumar says:

    thanks for this code

  5. Sai Prakash K says:

    Good explain ……..

  6. DEVI says:

    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?

  7. vladimir says:

    very helpful ex. – can you show image upload function ?

  8. ankita says:

    thanksa

  9. Nice code u have created….!!!

  10. Wanda Brian says:

    Thanks Prasad k for this helpful tutorial

  11. nazir ali says:

    nice and veryyyyyyyyyy helpful thankss

  12. sammy martin says:

    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.

  13. Martijn says:

    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.

  14. Hi,

    Thank you for your very understanding tutorial. Really helped me! Thumbs up.

  15. Ibrahim Aziz says:

    Thanks !, works like charm !.

  16. TJX says:

    Work well for me, thanks !.

  17. Gym says:

    Very nice tutorial ..easy to understand for all..Credit to admin

  18. Paul Walker says:

    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!

  19. wale says:

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

  20. bharat says:

    it is very easy for beginners.

  21. BHARAT GAANGADIYA says:

    IT IS VERY EASY FOR BEGINNERS

  22. Vinod says:

    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.

  23. Fuad says:

    where is the data base

  24. sanghamitra says:

    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

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

  26. Ajoo says:

    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

  27. Ajoo says:

    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

  28. Nelson says:

    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

  29. kb says:

    Thanks…….

  30. Monali says:

    very nice tutorial…..

  31. elihaki says:

    9c 1!!!i enjoy it

  32. preeti says:

    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

  33. suji says:

    thanks..

  34. Raj says:

    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.

  35. gasper kimola says:

    it awesome

  36. dhivya says:

    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

  37. prasana says:

    This s wat m looking for thank u soooooooooooo much

  38. sankar says:

    Good Tutorial For begginers

  39. sankar says:

    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

  40. thanks for this code

  41. varshini says:

    Good Job Friend. But Please help me …..How to edit the text which is in the textarea?

  42. Poojani Pathirana says:

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

  43. venkatesh lohar says:

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

  44. Jashu Ram says:

    Thank You. :)

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>