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;

CREATE TABLE users (
id int(11) NOT NULL auto_increment,
name varchar(100) NOT NULL,
age int(3) NOT NULL,
email varchar(100) NOT NULL,
PRIMARY KEY (id)
);

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.

config.php

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.

add.html

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.

add.php

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:

CRUD PHP MySQL

index.php

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.

edit.php

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

delete.php

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

Hope this helps. Thanks.

PHP

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • You should include database connection file at the top of your edit.php, like this:

  • Aini Asiqin

    Why delete operation doesn’t work for me ?

  • chamnan

    good

  • harivignesh

    i got the error like this :
    Notice: Undefined index: id in /opt/lampp/htdocs/harivignesh studies/currentupdate.php on line 59

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /opt/lampp/htdocs/harivignesh studies/currentupdate.php on line 63

  • Jephren Naicker

    How about adding A confirm delete ?.That would be Better, mistakes can happen

  • brok. Wayne

    It works just fine. Just carefully follow the step-by-step instruction and you will be amazed in the end that it works. It really does. Just make it work just now I mean AT THE MOMENT :)

  • Chungu Chenda

    Hai Just seen then looks great, but how do i put all the code in one file for example I put the code in index.php

  • Александр Шаповалов

    Could you help me Mukesh?
    I have a white screen when i try to use add or edit action.

  • Jelle Ruben

    My tables are empty on the index page, but there are records in de DB.
    And I don’t get a error of the database connection.

  • Jelle Ruben

    ..

  • Ali Ahmad

    How to inseert image in it

  • Ali Ahmad

    I wanna insert image z help me how

  • bella

    hai in add.php, index.php is referring what?

  • bella

    Great tutorial. thank you. just DELETE not function . I don’t know why.

  • bella

    very very very good tutorial

  • Brian Piring

    Name field is empty.
    “; } if(empty($age)) { echo “Age field is empty.
    “; } if(empty($email)) { echo “Email field is empty.
    “; } //link to the previous page echo ”
    Go Back”; } else { // if all the fields are filled (not empty) //insert data to database $result = mysqli_query($mysqli, “INSERT INTO users(name,age,email) VALUES(‘$name’,’$age’,’$email’)”); //display success message echo “Data added successfully.”; echo ”
    View Result”; } } ?>