Home » MySQL, PHP8 December 2007

CRUD with Login & Register in PHP & MySQL (Add, Edit, Delete, View)

In another article, I have written about Very Simple Add, Edit, Delete, View in PHP & MySQL. That article contains a basic CRUD (Create, Read, Update, Delete) system. It doesn’t have the feature of login and register.

In this article, I will be presenting a complete CRUD system containing login and register feature. User should register himself first. And then he can add data after logging in.

Here is the step-by-step guide on creating such CRUD system:

First of all, we need to create database and tables. Let the database name be ‘test2‘.

create database test2;

There are two tables in the system: login and products.

login: This table contains user’s information for login. It contains user’s name, email, username and password. User registration data is saved into this table.

products: This table contains data added by logged in users. This table contains product information like name, quantity, and price.

use test2;

id int(9) NOT NULL auto_increment,
name varchar(100) NOT NULL,
email varchar(100) NOT NULL,
username varchar(100) NOT NULL,
password varchar(100) NOT NULL,

CREATE TABLE products (
id int(11) NOT NULL auto_increment,
name varchar(100) NOT NULL,
qty int(5) NOT NULL,
price decimal(10,2) NOT NULL,
login_id int(11) NOT NULL,
CONSTRAINT FK_products_1
FOREIGN KEY (login_id) REFERENCES login(id)

Note: Cascading is done in products table making login_id as foreign key to login table. This means that all the products entered by a user is automatically deleted from products table whenever that particular user is deleted from login table.

Now, we will create a connection.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 test2 has been selected.

index.php is our homepage. In this page, if the user is not logged in then login and register links are displayed. If the user is logged in then the user is greeted and a link is displayed to view & add products.

Note: session_start() function should be written at the beginning of every php file where session handling is done. You can see the same in index.php as well.


index.php uses a little bit of Cascading StyleSheet (CSS). Here is the CSS file code used in index.php.


Registration page asks for user’s name, email, username and password. The registration data is saved in login table.


After successful registration, user needs to login in order to add products in the system.

When the login is successful, I have set three SESSION variables. You can see it in below login.php code.

SESSION[‘valid’] = This variable contains user’s username
SESSION[‘name’] = This variable contains user’s full name
SESSION[‘id’] = This variable contains user’s id


After successful login, user is redirected to homepage (index.php). There is a link to view and add new products.

To add product 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, quantity & price are empty or not. If they are all filled then the data will be inserted into database table.


Products data is fetched from database and displayed in view.php file. 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 view.php:


At the beginning of view.php file below and add.php file above, you must have noticed session_start() and isset() function.

session_start() function should be written at the beginning of every page where SESSION handling is done.

isset() function checks if the SESSION variable is set or not. If not, the user will be redirected to login page (login.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.


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 view page (view.php).


User can finally logout. User is redirected to homepage (index.php) after logout.


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

Hope this helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Pingback:   Very simple add,edit,delete,display in PHP — Chapagain.com.np :: Blog()

  • Pingback:   Very simple add, edit, delete, display in PHP — Chapagain.com.np :: Blog()

  • 345gfh

    good luck chap !

  • lokoHAN

    could you help me with my file system??..


  • Kevin Parula090


  • Nanerme69

    very simple.. yet… very effective.. thank you very much! 

  • Geloalfaro

    tnx a lot, hehehehe, big help,

  • Pratikpate

    how can i edit record which are already there in the admin panel

  • pyxis

    may I ask where is the edit function?

  • unknown king

    thanks lottt………….

  • nikita

    hello, I did my view, edit and delete php but its not working.. Can you help me ?? :(

  • nikita

    hai, I’m having problem in edit.php and delete.php… some errors in that code and I really dunno where is the mistake… Can you help me..??

  • That has been fixed. Edit, delete feature was missing. Now there are two versions of the code. The full version is available now with add, edit, delete features.

  • That has been fixed. Edit, delete feature was missing. Now there are two versions of the code. The full version is available now with add, edit, delete features. Please download it again.

  • AkseliP

    Hmm, some installation instructions? I get error: Could not execute the select query. plz help!

  • hjhjhk


  • Nikhil

    im beginner to php so will u please hlp how this are arrange to get a perfect form. eill u say r arrange

  • punit

    thanks v much

  • sai

    nice blog

  • jenelyn zepeda

    hi..im new to php and i have the error saying Access denied for user ‘root’@’localhost’ in config.php when i hit the the login button.i’ve donwloaded your full code in login,register,add,edit,delete.how i can fix this error?thank you!keep sharing :D

  • Safoku

    The reason you can access localhost because there is a password.Most localhost don’t have a password. Try removing it. ~Safoku@gmail.com for more questions. :D

  • Safoku

    I meant can’t.

  • safoku

    Try removing the password

  • VAruNA

    Thnx BRO

  • rae

    hi, 1st off thanks alot, but i dnt c the file database.sql

  • Sorry for the typo. The database file is login.sql.

  • creative network solution

    in Full PHP Code with all features: Login, Register, Add, Edit & Delete

    when we create data base then error shows

    SQL query:

    CREATE TABLE login (

    varchar( 20 ) COLLATE latin1_general_ci default NULL ,
    varchar( 32 ) COLLATE latin1_general_ci default NULL ,
    varchar( 50 ) COLLATE latin1_general_ci default NULL ,
    PRIMARY KEY ( id )

    ) CREATE TABLE users (

    varchar( 100 ) NOT NULL ,
    int( 3 ) NOT NULL ,
    varchar( 100 ) NOT NULL ,
    PRIMARY KEY ( id )


    MySQL said:

    #1064 – You have
    an error in your SQL syntax; check the manual that corresponds to your MySQL
    server version for the right syntax to use near ‘CREATE TABLE users
    id int(11) NOT NULL auto_increment,
    name varcha’ at line 9

    how we solve this problem

  • bhavin

    thank you… very simple & effective work sir..!

  • NightFury

    Thank’s a lot buddy.. It helped me a lot. I would love to see more of these posts from you..

  • Krad

    excuse me but ive been getting an error and ive been having a hard time on it

  • This code should be at the top of your page:

    Please check if there is any whitespace or other code before the above session_start code.

  • Krad

    thank you~

  • pema

    if i was to avoid the duplication of data in the table , how can i do ??? in your exact coding

  • pema

    if i add data more than once, error message should be given… so how can i do ..?????

  • Yusra Shaikh

    i had a problem in the login and register page…when i add the data and click submit, i get this error
    mysqli_query() expects parameter 1 to be mysqli, null given
    i am getting this error at line number 22 of register.php

    can u pls help?

  • Kerneel Leonil Bagual Laus

    Parse error: syntax error, unexpected ‘{‘ why I always have an error like this with your codes? tnx for the response :)

  • Your code on line 22 of register.php should be like this:

    mysqli_query($mysqli, "INSERT INTO login(name, email, username, password) VALUES('$name', '$email', '$user', md5('$pass'))")

    $mysqli is the connection object from connection.php file.

  • At which page are you having such error?

  • Play Boy

    Hi guys! really nice and helpful tutorial found on this website but I’m having a little bit problem, I can login and can add the data but when I edit data then it’s give some kind of errors. As I’m new in PHP so I’m not sure how to handle this problem, can anyone help me or guide me please! thank you so much.

    1) Warning: Cannot modify header information – headers already sent by (output started at C:xampphtdocsCURD_Loginedit.php:3) in C:xampphtdocsCURD_Loginedit.php on line 40

    2) Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:xampphtdocsCURD_Loginedit.php on line 51

  • ahmad

    error at login.php at line 38, but the data was inserted.. what is worng?

    by the way tahnk you very much :D

  • Hussam Jumah

    propblem solving plus,delete and modify