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

Click here to Forgot Password Recovery (Reset) using PHP and MySQL

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.

119 Comments

  1. Hi, thanks for the code, however, when I enter data into the insert page, it returns a blank page and the view records table does not reflect the entered data, how can I fix this, thank you

  2. I am receiving the following error sometimes not all the time can you advise if i send you what i am doing please:

    Dashboard | Insert New Record | Logout

    Update Recipe Record

    Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\download1\demo\edit.php on line 64

    you can see whats happening in the test website:

    regards Steve Jones

    1. I did check and i didn’t find anything wrong, i think you should check line no 64 and see what is missing over there, i think you should run my code separately first, if it is working separately fine then you should integrate it with your module.

    1. For this purpose you must need to make email as unique ID and allow users to change their username, add additional column with name username that they can change easily based on user email address.

  3. Unable to connect
    Firefox can’t establish a connection to the server at localhost.

    The site could be temporarily unavailable or too busy. Try again in a few moments.
    If you are unable to load any pages, check your computer’s network connection.
    If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

    I am getting this error.can u help me to solve it??

    1. Richa browser does not have anything with the database connection, you need to enter your credentials in db.php file, if you do not know how to connect database using php, then you can search on Google for database connection examples.

  4. Hi sir i’m a 3rd yr student! How to solve these type of errors from the above coding?
    “”Warning: require(db.php): failed to open stream: No such file or directory in C:\xampp\htdocs\login\insert.php on line 2

    Fatal error: require(): Failed opening required ‘db.php’ (include_path=’C:\xampp\php\PEAR’) in C:\xampp\htdocs\login\insert.php on line 2″”

  5. Respected sir when i insert the name and age in insert.php it show the Warning: mysqli_error() expects exactly 1 parameter, 0 given in /opt/lampp/htdocs/php/insert.php on line 13.all code same as your.
    please sir give the solution of this warning. Thanks

  6. Javed I’m facing Error about the edit button.

    <input name="ID" type="hidden" value="” />
    <input type="text" name="Product" placeholder="Enter Product" required value="” />
    <input type="text" name="Price" placeholder="Enter Age" required value="” />
    <input type="text" name="Quantity" placeholder="Enter Age" required value="” />

    When I run the html It suddenly go to Auth.php

    1. It Seems like that you changed the names of input fields, first i will suggest to run the tutorial same as i shared with you, once you understanding working then you can modify it for your need.

  7. HI Javed
    help me
    how to display data based on username
    and how to maintain when the same username and email registration then can not register and there is a forgotten password menu

  8. Hi Javed, love the tutorial.

    In the ‘insert.php’ code, does the following code cause or have an effect on an undefined index below such as ‘name’:

    if(isset($_POST[‘new’]) && $_POST[‘new’]==1){

    Also does this need to be edited if I add more inputs to the form>?

    Thanks in advance

    1. No it does not need to be edit, you can any number of fields but if you remove the above condition so you may have to face undefined index issue, because it checks either form is submitted or not, if form is submitted then we get the values and insert them. If you remove it then when you will open this form it will try to find variables which are not available so you have this error.

          1. Thanks for clarifying I wasn’t sure if that counted as security against SQL injection.

            Can you help me with a way of adding password hashing with password_hash and password_verify?

  9. Javed Ur Rehman I have register user every time. But I am not able to login on your demo profile. Are you sure your code is working fine. Because I am a developer. Please check again and let me know what is the issue.
    Thank you for share this code. But Please do correct…
    Then it will very help full for another guy. I don’t mind
    Thanks ..

  10. 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 ???

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

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

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

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

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

    2. Hi. I have Warning: session_start(): Cannot send session cache limiter – headers already sent (output started at /public_html/admin/login.php:1) in /public_html/admin/login.php on line 12

      DB connection is fine

      1. Well it seems like you are getting error in DB connection, therefore you are getting error of “headers already sent” which means db connection failed and it generated html code, therefore session_start() giving error.

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

  17. 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👍👍👍👍👍

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

  19. 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?

  20. 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 ?

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

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

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

  24. 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??

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

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

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

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

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

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

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

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

  33. 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 *