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 NAME : freeze_demo
TABLE NAME: addd
FIELDS: id(primary key with auto increment), name, age
[code type=css]
<style type=”text/css”>
td
{
padding:5px;
border:1px solid #ccc;
}
</style>
[/code]
PHP Code to Add Edit List Delete Record
Here is the simple php code for add, list, edit and delete record
[code type=php]
<?php
$query=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“freeze_demo”,$query);
?>
[/code]
[code type=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>
[/code]
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
[code type=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>
[/code]
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.
[code type=php]
<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>
[/code]
When delete (‘X’) option is clicked in list page, particular record will be deleted by using id values and redirected to list.php
[code type=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>
[/code]
can you help me? i have a project in web development.. database using php..
it is about billing..thanks
nice it is very useful for beginners.
where is the download link????
Copy the above code and execute.
thanks for this code
Good explain ……..
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?
try again it must work
very helpful ex. – can you show image upload function ?
thanksa
Nice code u have created….!!!
Thanks Prasad k for this helpful tutorial
nice and veryyyyyyyyyy helpful thankss
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.
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.
its just a simple php code, we have to add validation, session etc
Hi,
Thank you for your very understanding tutorial. Really helped me! Thumbs up.
Thanks !, works like charm !.
Work well for me, thanks !.
Very nice tutorial ..easy to understand for all..Credit to admin
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!
I just given the basic code. We have to use Client side validation to ensure security.
i have web project,but my rigster page only rigster one data ,what is the problem?
check on strings
thanks
it is very easy for beginners.
IT IS VERY EASY FOR BEGINNERS
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.
use checkbox value as user’s primary key(id) and using that id value perform delete operation
where is the data base
DATABASE NAME : freeze_demo
TABLE NAME: addd
FIELDS: id(primary key with auto increment), name, age
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
Please check mysql query, post ur code here
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
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
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
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
good
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
Thanks…….
very nice tutorial…..
9c 1!!!i enjoy it
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
Please check, whether you have included database connection
thanks..
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.
it awesome
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
Error is corrected, Please check now
This s wat m looking for thank u soooooooooooo much
Good Tutorial For begginers
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
thanks for this code
Good Job Friend. But Please help me …..How to edit the text which is in the textarea?
I have try this for textarea also…It works….Thank you so much….
Very useful tutorial for the beginers.thank you so much….
thank you,
nice explanation. this is more useful for beginners.
Thank You. 🙂
Thank you very much, you have done a great job. Stay bless
IT IS VERY USEFUL AND INTRESTING
Its really helpful.
Thank you very much..its really helpful..thanks again..
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 !
thanks
Very useful for the beginners. Thanks a lot
Thanks for the very useful coding
thank you so much bro…
Thanks a lot
THANKS FOR YOUR CODE
thank you sir
Thank you this coding helped me a lot
Thankyou so much. It’s very nice