This is Part 2 of a tutorial designed to teach you how to create a basic CRUD (Create, Read, Update & Delete) interface for a MySQL table using PHP. Part 2 covers the Read page. This part is super simple, don’t worry.

Part 1: Create (Add)

Adding a Menu

Before we create our second page, let’s add a menu so we can easily access our pages. Save this code as menu.php

Now in your add.php file, before the HTML form, add this line:

Creating a config file

The database connection is something we will want to use on every page, so at this point it’d be a good idea to move it into a separate file which we can include in the others. We can also put the array of fields in there, since we can use that same array for our table on the View All Entries page. Replace the code in your add.php file with a require as well.

Reading Entries

For our index page of our contacts CRUD application we’ll list all the entries in the database. This part is fairly simple, we’ll just select all the rows, and echo out an HTML table. Don’t forget to include the config file and the menu.

Adding Edit/Delete Links

The only thing we have left to do is add some links to the HTML table to let you edit or delete a row. For this part we’ll need to add the unique id for the MySQL table to our fields we select, and add two columns to the HTML table for the actions. (Check the line numbers if you’re confused about where to put this code)

Select the ID

Add the Edit/Delete Columns

Add the columns to the table headers…

…and to each row

Part 3: Update (Edit)

, , , , , , ,


  1. Great tutorial

  2. Glenes Lincaro @ 2013-06-21 04:24

    I’m getting error in index.
    Notice: Undefined index: contact_id in C:\Program Files\wamp\www\practice\PDO\index.php on line 40

    it did not able to read the contact_id field in the database.

    Im new in PDO Im not familiar editing the query using PDO.

  3. Clear and concise – thank you for the time and effort you have put into creating this series.

    I have a question about the list displayed in the table. I have the table headers of First Name, Last Name etc., but each row of data has it’s contact_id displayed in the First Name column which means that all entries are moved over by one. I edited config.php to have the first line in the array ‘ID’,> which created an ID column but this also added an ID field on the Add New Contact and Update Contact pages. When I tried to add a new contact, an error was generated (I have enabled error reporting temporarily).

    How can I edit your code to make sure that the entries are displayed correctly?

    Thanks for your time.

  4. Oops – the new entry I set in config.php hasn’t displayed correctly. It was:


  5. The contact_id value is displayed, but no header is displayed….

    I would suggest to not display it at all:

    if($field != “contact_id”){
    echo ” . (isset($row[$field]) && strlen($row[$field]) ? $row[$field] : ‘&nbsp’ . ”);

  6. Thank you for the suggestion. I agree that it’s better not to show the contact_id in any of the rows rather than have ID shown as the first column header. I just didn’t know how to suppress display of the contact_id!

  7. There was an error on line 31 of the index.php – this has been corrected.
    It should read:

    Making the cells show only the fields we desire. Hope that helps!

  8. There was an error on line 31 of the index.php – this has been corrected.
    It should read:

    Making the cells show only the fields we desire. Hope that helps!

  9. Thank you for the update. I realise that minor typos can creep in and am grateful that this has been recognised then corrected quickly. I’ve seen plenty of blogs where code has been buggy and no-one has responded to questions or comments from readers. All that has done is make me not return to the blog to investigate further topics!

  10. if you getting undefined index for contact_id..
    $fields_str = ‘'.implode(array_keys($fields), ', ').'‘;
    $fields_str = ‘contact_id, '.implode(array_keys($fields), ', ').'‘;

  11. Hi, great tutorial, thanks.
    Can you give an example for if you wanted to add select options to the forms? eg: instead of just having a field for ‘state’, if you wanted to use a pull down select list to choose a state.
    Do you have an example on how to incorporate this?

  12. Hi I’m encountering the problem

    “Invalid argument supplied for foreach() in ”

    My code is

    foreach($flds AS $setsumei){
    $flds_str = ‘'.implode(array_keys($flds), ', ').'‘;
    $sql = “SELECT $flds_str FROM ‘gamekyara’”;
    $flds = array(
    //This is where the error points
    foreach($setsu->query($sql) as $row) {
    foreach($flds AS $fld=>$value){
    $kyaraStat.=”.(isset($row[$fld]) && strlen($row[$fld]) ? $row[$fld] : ‘&nbsp’.”);

    what seems to be the problem?
    also i put the array declaration of fld inside index.php,
    it doesn’t seem to work inside a separate config.php file

  13. I’d like to display the data in a column style like this


    field data

    id | 1001
    —– ————
    name | Yoshitsune
    —– ————
    class| Bushi
    —– ————

    I’m successful on displaying the headers using the array you taught above, my only problem is displaying the data

    As for the part of foreach,
    the part

    echo ”.(isset($row[$field]) && strlen($row[$field]) ? $row[$field] : ‘&nbsp’.”);

    instead of echoeing it, i plan to store it in a variable for a while

    so I wrote

    $kyaraStat.=”.(isset($row[$fld]) && strlen($row[$fld]) ? $row[$fld] : ‘&nbsp’.”;

    which yields the error
    Parse error: syntax error, unexpected ‘;’

    sorry quite new to php

    i didnt get the gist also on implode
    here’s a portion of my code

    $flds = array(

    foreach($flds AS $setsumei){

    $flds_str = implode(“,”,$flds);
    $sql = “SELECT $flds_str FROM gamekyara;

    foreach($setsu->query($sql) as $row)
    foreach($flds AS $fld=>$value){
    // if the field is blank, we want to empty a blank space, otherwise the HTML won’t work properly
    $kyaraStat.=”.(isset($row[$fld]) && strlen($row[$fld]) ? $row[$fld] : ‘&nbsp’.”;

Add your comment now