Home » MySQL, PHP5 March 2008

Very Simple Add, Edit, Delete, View (CRUD) in PHP & MySQL [Beginner Tutorial]

In this article, I will be presenting simple PHP & MySQL code to add, edit, delete and view data. This kind of system is also referred to CRUD (Create, Read, Update, Delete).

Here is a step-by-step guide on creating a CRUD system using PHP & MySQL:

First of all, we will create a new MySQL database. Let us name the database as ‘test‘.

create database test;

Then, we will create a new table in database ‘test’. Let us name the table as ‘users‘.

use test;

id int(11) NOT NULL auto_increment,
name varchar(100) NOT NULL,
age int(3) NOT NULL,
email varchar(100) NOT NULL,

Now, we will create a config.php file which contains database connection code. This code connects to the MySQL database. This file is included in all PHP pages where database connection is necessary.


In below code, the database host name is localhost where username=root and password=root. The database test has been selected.

To add data into database, we need an html form.


Form action on add.html is add.php. It means that the submitted form data will go to add.php. In add.php, we do a simple validation of checking if the entered name, email & age are empty or not. If they are all filled then the data will be inserted into database table.


Data from database is fetched and displayed in index.php file. This is our homepage. This file also contains a link to add data. On every row of displayed data, there is also a link to edit and delete data. Below is a sample image of our homepage:



Each row of data can be edited separately. Row ID is passed in the URL of edit.php. ID uniquely identifies the data entry.

While adding data, we had two files: add.html and add.php. While editing data, I have kept the entire thing in a single edit.php file. Edit form in HTML and database update code in PHP are present in the same file.

In the code below, at first a single row entry of data is fetched based on the id. The fetched data is displayed in the edit form. When user edits the data and submits the form, then some simple validation is done for empty data. When everything is correct, then that particular entry of data is updated in database.


Each row of data can be deleted separately. Row ID is passed in the URL of delete.php. ID uniquely identifies the data entry. After deletion, the user is redirected to homepage (index.php).


Download Full Source Code: Simple Create, Read, Update, Delete in PHP & MySQL

Hope this helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Michael

    Thanks for your help sir….

  • Jsquad2008

    thak you sir for your kind!…

  • Bulbul4u_only


  • Avinash Powar

    This is very good but how can i edit if my data type is TEXT & textarea control ?

  • Thalhah Amir


  • Trebor

    many thanks for posting this tutorial. Question please, for me Edit page only displays first six or so characters in name, age and email fields. Is there a way to display more or all characters?

  • Trebor

    many thanks for posting this tutorial. Question please, for me Edit page only displays first six or so characters in name, age and email fields. Is there a way to display more or all characters?

  • Priyanshu Mittal

    hey thts a coll example thnk you helped me alot

  • Cindie Kato

    tnx dude

  • Ravi

    Thanks Mukesh,
    Your code is very helpful , I am creating a small client server application with database.
    Users(with different login) can submit their daily data and can see and edit them .
    admin can see all the users and generate report.

    I am still in process , but your code has help me a lot in user page development.

  • Srinivas4146

    good tutorial for learners

  • Pingback: Very simple add, edit, delete, display in PHP « PHP MySQL()

  • Lucky

    Thx So much ………..u r great…..

  • Beng Ngim

    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:wwwcrud-simpleindex.php on line 24

    How to fix it? Plz…

  • User

    How can I add pagination please help.

  • It should work without warning. Have you already added data into the database table? You may use mysql_fetch_assoc instead of mysql_fetch_array and test as well.

  • Tef Fy

    Thank you for the tutorial. Simple tutorial like this it’s very appreciated. Please keep posting.

  • Manda Mirza

    helo can you help me in this task tef ?

  • Hwaiting 93

    thank you so much .. great tutorial



  • Yogesh Satale

    Great examples for beginners….its Very accurate example which is very easily execute by us. Thanks for this knowledge sharing. and Happy diwali to you and your family.

  • woutie

    Nice tutorial, i was looking for the confirmation before deleting. Worked like a charm ;)

  • Vrajesh Savaliya

    thnks same code but plz add gender=radio,hobbies=checkbox,city=dropdown list & date plz in this field create code & same edit,update & delete code

  • রাসেল

    mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:xampphtdocscrud-php-complete-masterview.php on line 34 What’s the problem? plseas help me .

  • Hany Salama

    Please, how do I pass variable from 1 view form to another. Eg countryid from countries table to 2nd form (states). Thanks


    how i fetch and display image into the table in index.php… pls help

  • kate

    Hi, the form duplicates every time I refresh it.

  • prasanna

    its amazing..easily i understand insert and update delete functions…super bro

  • surma

    dear, sir please add image function in this script
    then we will be regretful to you

  • tweetpop

    omg ur a life saver, been looking all over for something similar and i was starting to think it was impossible lol so happy:)

  • Raymond Chok

    i found some bug when i’m trying this, for some reason if my data in database contain space, when i edit the textbox will only recall whatever data before the space. Is there any way to fix this?

  • nitin kumar maurya

    this code not work

  • nitin kumar maurya

    Edit .php not work in my system why ?
    plz solve my question.

  • Sachinsingh

    Its very very effective and with error code…Developer Mukesh Bhai…keep coding and Direction…

  • The edit.php code has been updated. It should solve the issue.

  • Edit.php code has been updated. It should solve the issue.

  • Edit.php code has been updated. It should solve this issue.

  • kenneth

    when i will add a new data it will not appear and also in the database? what should i do?

  • mixing view, model and controller? barbaric

  • angel

    $sql =”UPDATE datatable_name SET where id=$id “;

  • Joseph Muiruri

    You My Friend are Simply a Genius. After many days of frusration on how to edit records, I finally stumbled this. It worked Magic!!! Thank You for the clear instructions and a selfless dedication to make your knowledge useful. Good Day.

  • Maurizio Omissoni

    thank you for your efforts

  • Ikenna Brendan

    Thanks for the posting, however, i am new and would like to know how to add multiple information at the same time and not one by one. That is, adding more names, ages and emails at a time.

  • jui hande

    thank u so much for the effort ..

  • deepak


  • Meo Thai

    I have problem with
    Notice: Undefined index: id in C:xampphtdocsphpsimpleedit.php on line 44
    Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:xampphtdocsphpsimpleedit.php on line 47
    how can i fix it?

  • i do not see in your example how the html can have the edit and delete together. in my table i have edit and delete – each with its own form, but the edit only shows last record value; delete shows it’s respective values/works. forms wrapped around one another causing problems? also, posting to my same page, not using external php files. so having simply like this”if (isset($_POST[‘fund_data1’])) {//Update” please post code that shows it all working together. thx!