This tutorial will teach you how to create a basic CRUD (Create, Read, Update & Delete) interface for a MySQL table using PHP. We’ll be using the PDO extension to handle the database interaction, so this tutorial will allow you to create an interface that is safe from SQL injection.

MySQL Table

We’ll create a basic table to store our data. For this tutorial, we’ll store some simple data, a contacts list to store our friends and family’s names, addresses and phone numbers. I’ll be using http://www.generatedata.com to generate random data, this is a great website to use for generating random sets of data.

Create (Adding) Entries

Create a new php file called add.php – this will be our form for adding new contacts.

PHP/HTML Form

First, let’s create an array of the fields we want to capture, along with their labels.

Then, we’ll use PHP to generate an HTML form with an input element for each field. Using PHP to generate the HTML form allows us to easily add new fields later, and add in error messages, without a lot of repetitive code. I’ve also added some styling. We’ll use the error class later.

Form Processing/Validation

Now we need to add the PHP code to process the data. For this tutorial we will only do some basic validation, making the first and last name required. After we validate those fields, we can check if it’s okay to insert. If it is, first we will prepare a statement (using positional placeholders), then insert the values. We’ll store the result of the insert (a boolean) in a variable to check later.

First, let’s add the validation. This code should go before your form HTML, but after the array that has the fields in it.

Now we’ll add the error messages to the form, as well as filling in the previously submitted data if any exists, so if there was an error the user doesn’t have to retype everything. Replace your previous PHP for generating the form with this:

Test the form out by entering a phone number or address, but leaving one or both name fields blank. You should be able to see your error messages on the first and last name fields, and keep the data you already entered.

Inserting Data to MySQL

At this point we are ready to insert the data. Add your database connection to the top of the file. We’ll be using PDO.

Then go to the end of your form validation code. If there are any errors, we don’t want to insert, so we want to check if there are no errors. If there are no errors, we’ll create a prepared statement and insert our values.

Success/Error Message for Insert

Finally, we’ll use the $result variable to display a success message if the form was submitted, or an error message including DB error info if not. Remember to remove the database error message code before using your code on a production server.

Final Code

Here’s what the final code looks like altogether.

Next Steps

Go ahead and add a few rows worth of data. We’ll work on the Read step next, so you’ll want to have at least one or two rows.

Part 2: Read

, , , , , , ,
Trackback

3 comments

  1. This is really great! Thanks for sharing.

  2. I was wondering how you would make a dropdown in the form. I need to have dropdown and am not sure how to incorporate this into your script. I have all of the fields in the config file. Awesome! so much easier to ADD fields. but I can’t figure out how to make one of them as a dropdown list of items and 1 HAS to be chosen. Thank you so much for making this so much more understandable.

    Shirley

  3. Thanks for sharing but I would prefer to style the form myself, without automatically generating the fields. Could you show how to incorporate the last section of the php into a form in which the fields have been entered manually? e.g.

    Registration Form

    Please enter your forenameForename

Add your comment now