Insert, View, Edit and Delete Record from Database Using PHP and MySQLi

Demo Download

In this tutorial i will explain how to insert, view, edit and delete record from database using PHP and Mysqli, basically this tutorial is a second part of Simple User Registration & Login Script in PHP and MySQLi, in this first part i explained how to create simple user registration and login using PHP and MySQLi, if you do not know how to create user registration and user login form so kindly first check this tutorial Simple User Registration & Login Script in PHP and MySQLi, after that now come back to this tutorial.

I am using PHP 5.6 for this tutorial, i didn’t check this tutorial on PHP 7, kindly make sure you are using PHP 5.6 to avoid any unexpected error.

Steps to Creating an Insert, View, Edit and Delete Record from Database Using PHP and MySQLi

Now i am assuming that you have already created user registration and login forms which i created in Simple User Registration & Login Script in PHP and MySQLi now i will create another table to keeping records in it, update dashboard.php file and add four more new pages which are insert.php, view.php, edit.php, and delete.php, follow the following steps:

  1. Create Another Table for Records
  2. Update Dashboard File
  3. Create Insert Page
  4. Create View Page
  5. Create Edit/Update Page
  6. Create Delete Page

1. Create Another Table for Records

Execute the below SQL query:

2. Update Dashboard Page

Update dashboard.php file and paste the following code in it.

3. Create Insert Page

Create a page with name insert.php and paste the below code in it.

insert-new-record

4. Create View Page

Create a page with name view.php and paste the below code in it.

view-record

5. Create Edit/Update Page

Create a page with name edit.php and paste the below code in it.

update-record

6. Create Delete Page

Create a page with name delete.php and paste the below code in it.

Demo Download

If you found this tutorial helpful so share it with your friends, developer groups and leave your comment.

Article By
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.

88 Comments

  1. Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/ubuntu/workspace/bearbeiten.php on line 17

    I dont know how to fix this :/ can you help me plz ???

  2. i got this error but i do not know how to solve this problem
    Notice: Undefined index: ic in C:\xampp\htdocs\OnlineRegister\delete.php on line 4

    1. There is no need to browser delete page, if you browse it will give error because we need id to get and use to delete data of that id, if you open this page directly it need id, and it is not able to find id therefore it will give you error, better you just use it in view page as i used.

  3. Warning: mysqli_error() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\cnd\edit.php on line 35

    Hi

    I am getting this error on edit page

  4. what does the $con stands for? what does it define ,it is found on the edit page and $result = mysqli_query($con,$sel_query);
    while($row = mysqli_fetch_assoc($result)) { ?>
    what does the above code fine?

    i also have problems with the insert.php with this code ,it gives error saying something about column 1 and denies to insert data into database

  5. Dear Sir,

    please send reply for bellow points.

    how to i have md5 to string convert password in php and codignator.

    and one site complete in WordPress but i have first open my static html page then redirect main site how to do it.

  6. your way of explaining is wonderful and practical. Every thing working well. My doubt is that using login code, it is not directing to any page. screen becomes clear and no link appears.

    i am getting following error
    [09-May-2018 18:43:08 Asia/Calcutta] PHP Warning: session_start(): Cannot send session cache limiter – headers already sent (output started at /home/pctant/public_html/editing_data/login.php:10) in /home/pctant/public_html/editing/login.php on line 12

    1. Check your database connection, because if you got error in DB connection, it will print DB error message, which will appear before session_start(). SO if anything print before session_start() you will get these kind of error.

  7. Good day,
    Please i am having this issue, i have a site with user account, when i log in as admin i cannot edit users profile.
    I tried to add edit button on the profile page, but when i edit it does not change the value.
    I added ModifyPhone.php, i edited process.php, also edited the profile index page, what else do i do so the change will be saved and reflect on the user details

    1. Gary kindly check update query, as update query help you to update data in your table, better you should try your query on sql direct, if that is not updating in direct sql then you should first fix your sql query to reflect the update.

      1. can u help me do inventory system for ict equipment in hospital just do insert,view, update and delete function…

        1. Dear Nurul, currently i do not having and such tutorial, however you can use my above code script for all insert, view, update and delete function, all you need is just to change fields name and data as per your need.
          Thanks

  8. I just came to learn only Insert data on MySQL Database, but because of your guide, I learn easily to update database too.

    Thank you Javed.

    Big Thumbs Up👍👍👍👍👍

  9. when i try to log in then is shows me this error plz help me

    mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\php crud\login.php on line 28

  10. Thanks a lot for the time and effort you put in to make this tutorial easy to understand. I really benefited from it. Please I have one question. Everything is working fine but it still shows undefined parameter. Mysqli requires one parameter. It shows it on the edit page.. Could it be because I’m not yet logged in?

  11. 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 ” where id=’1” at line 3

    i got this arrow. can u explain it ?

  12. Good Job.
    But it seems you only fetch the last record in the databse to be display in the view.php using “ORDER BY id DESC.
    Thanks.

  13. 1.Notice: Undefined index: id in C:\xampp\htdocs\practice\delete.php on line 3

    Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\practice\delete.php on line 5
    HOW TO FIX IT But the code is same as ur
    2.when i am running these modules it redirect me on the login page
    how to fix it….

    1. Check if your session is set or not, at the time of logged in $_SESSION[‘username’] is set.
      Also make sure you are redirected to the index page, check the address bar URL must contain /index.php
      if you do not see index.php after log in this mean you are not redirecting.
      I will advise you to check this code on both local machine and web server, sometimes this happen due to PHP version issue.

      1. Can you direct me to a page that will show me how to NOT duplicate email addresses when users try to sign up? In other words, if the email is already in database show error and reset their password if they like. Thank you for great tutorial.

  14. i want to let you know that i have tried several tutorials but this your tutorial is the BEST , I mean THE BEST , no error, no hidden code, , man You are good, May God bless you for me

  15. 1. Create Another Table for Records

    Execute the below SQL query:
    CREATE TABLE IF NOT EXISTS new_record (
    id int(11) NOT NULL AUTO_INCREMENT,
    trn_date datetime NOT NULL,
    name varchar(50) NOT NULL,
    ageint(11) NOT NULL,
    submittedby varchar(50) NOT NULL,
    PRIMARY KEY (id)
    );
    1
    2
    3
    4
    5
    6
    7
    8

    CREATE TABLE IF NOT EXISTS new_record (
    id int(11) NOT NULL AUTO_INCREMENT,
    trn_date datetime NOT NULL,
    name varchar(50) NOT NULL,
    ageint(11) NOT NULL,
    submittedby varchar(50) NOT NULL,
    PRIMARY KEY (id)
    );
    Please how do i do this command in cmd or where??

  16. Are you sure the insert code is correct?? seems to have no relation to the database ‘registers’ that we create at start of tutorial .

  17. Hi i cant get your connect db to even work so i changed with one i use and it connected.,then every page is full of errors like:(view page)
    mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64

    and in (insert page)
    Parse error: syntax error, unexpected ‘$trn_date’ (T_VARIABLE) in C:\wamp64 etc

    looks really good as well,,,pity i cant get it to run smoothly.

  18. Thanks for your great article. It cleared my many doubts. As did all as you mentioned but after creating a user when i click submit and login it stays on login.php page and shows whole page white. it doesnt go to index.php page. Could you please help me this as i am stuck at this stage.

  19. hi, first off, thank you for the clear code. One problem though, when I try to add a user and click the submit button, it goes to a blank page but does not add the user. I have tried to look for the problem for two days now with no success. kindly help.

  20. As Salam Alaikum, do you think it is possible for there to be a page where the user may only be able to modify their own details? e.g. change only their own password.

  21. thank you very much for helping me out start over again with latest mysqli command. if any inquiry may i know your email?

  22. I am very new to PHP and MySQL, just copied your code and run everything works perfectly fine. Please can you post code on how to insert more than two records into the Register database?

    I made attempt to add more columns as Gender, Phone Number, Address, etc to be displayed when view.php was executed but ended up getting error message. Please assist.

  23. Hello, I have noticed that it is possible to delete a record even though it is not logged in, because you do not call auth.php, so just enter the url and any ID and the record will be deleted by external people. It’s easy to fix but it’s important to warn. Sorry for spelling. Thanks, Alexandre Schmitz

  24. Strict Standards: date() [function.date]: It is not safe to rely on the system’s timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘UTC’ for ‘8.0/no DST’ instead in C:\AppServ\www\file1\edit.php on line 27
    how to solve this one/

Leave a Reply

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