Inserting Data Into a MySQL Database using PHP
This tutorial is a continuation on the “How to Access a MySQL Database Using PHP” tutorial that showed you how to setup a database using phpMyAdmin and how to read data from the database using PHP. In the tutorial I will show you how to write data to the database directly through a from on your website. This is going to be a very basic writing tutorial. We will get into more advanced stuff later on.
You will need to follow the first part of the the “How to Access a MySQL Database Using PHP” tutorial to setup the database and user permissions. After you do that you can continue with this tutorial.
At this point you should have the database setup with some test data in it. I am going to show you how to update the date by using a form on a website. First we will create the form. You can use any editor you would like. Sometimes I use notepad, but for this tutorial I will be using cPanel’s web base editor. It allows me to edit the files directly on my server and also color codes the lines nicely.
First we will create our form. We will name it “form.php”.

Now we will create the form using some basic html, nothing fancy. Start with the form tag. Notice that the form action is set to update.php, that is the script that will update the database.

Now we will add 3 input boxes for our 3 columns in the database. FName, LName, and PHON. There is a 4th column in the database we created, the ID field. That field is auto generated so we do not need to update it.

We created the input boxes for each field in the database. You will notice that the size of each input is limited to the size of our fields in the database. Each field is a text because we want a text box.
Next we need to add a submit button.

A submit button is also an input field. The type is submit, which will create a button and the value gives the button a label that you will see from your browser.
That is it for form.php. I might as well mention that there is no reason that I named this with a .php extension. It could have been called form.html and it would have functioned the same way because there is no php code on the form. Save the fiel and open it in your browser. It should look like this.

The next step is to create the update.php file. This file will update the database with the new information. Create a new file called update.php.

Now to start coding a php file we always start with the open and close php tags.

The first thing we are going to do is capture the data that was sent from the form.php file and store the info as variables. Because the form method was set to post, we can use the post variable function to collect the data. You will notice that the names in the post function will match the names we used on the form.

Now we will make a connection to the database. You will have to replace:
“localhost” with the location of your server
“projectc_testuse” with your username
“password” with your password
Then we are selecting the “projectc_Test” database you will change that to whatever you named the database in the first tutorial.

Next we are going to create the query that will insert a new row in the database. As you can see we are inserting in the table called TestTable and were are inserting the fields ID, FName, LName, and PHON. The actual values are coming from the variables. The ID fields is left NULL because it is auto generated by the database.

Now all we have to do is have the query execute. Then if the query fails we will print an error. If it is successful we will print the info that was added to the database.

Now you can test out the form.

When you submit the form you should see your results displayed.

Now if you run the php file from part one of this tutorial you should see you updated info in displayed along with everything else that was in the database.

So that is a very basic way of writing to a MySQL database using PHP. Now you should be able to create a database, write data to the database, and read from the database. I hope to create some more advanced PHP/MySQL tutorials in the future.
Pingback: Convert a MySQL date field using PHP Functions | Team Tutorials
Pingback: Editing MySQL Data Using PHP | Team Tutorials
Pingback: Writing to database? - x10Hosting Community
Pingback: PHP Tutorials » Using PHP to update a database
Pingback: Using PHP to update a database « Tutorials
Pingback: Using PHP to update a database || Tutorials
Pingback: PHP Code » Blog Archive » Using PHP to update a database
Pingback: PHP Code : Using PHP to update a database
Pingback: Using PHP to update a database « Computer Help Tutorials and Tips
Pingback: phpcode » Using PHP to update a database
Pingback: Tutorials » Blog Archive » Using PHP to update a database
Pingback: Tutorials » Using PHP to update a database
Pingback: programmingandcomputertutorials
Pingback: Computer Help Tutorials and Tips » Blog Archive » Using PHP to update a database
Pingback: Using PHP to update a database | PHP Code
Pingback: Using PHP to update a database | Tutorials
Pingback: Balıkesir Üniversitesi Tutorials
Pingback: PHP Code » Using PHP to update a database
Pingback: CW Connect — Blog — Using PHP to update a database
Pingback: Using PHP to update a database « phpcode
Pingback: Programming and Computer Tutorials » Using PHP to update a database
Pingback: » Using PHP to update a database
Pingback: Using PHP to update a database
Pingback: Using PHP to update a database « computerhelptutorialsandtips
Pingback: tutorials
Pingback: Learn Computer Tutorials » Using PHP to update a database
Pingback: phpcode › Using PHP to update a database
Pingback: Tutorials » Using PHP to update a database :: iBlog
Pingback: Computer Help Tutorials and Tips » Using PHP to update a database
Pingback: Using PHP to update a database « Programming and Computer Tutorials
Pingback: Booned — Blog — Using PHP to update a database
Pingback: Using PHP to update a database « Learn Computer Tutorials
Pingback: Learn Computer Tutorials » Blog Archive » Using PHP to update a database
Pingback: programmingandcomputertutorials » Blog Archive » Using PHP to update a database
Pingback: Using PHP to update a database « computerhelp
Pingback: Using PHP to update a database – phpcode
Pingback: Using PHP to update a database @ PHP Code
Pingback: Using PHP to update a database « learncomputertutorials
Pingback: tutorials › Using PHP to update a database
Pingback: phpcode » Blog Archive » Using PHP to update a database
Pingback: » Using PHP to update a database tutorials
Pingback: Programming and Computer Tutorials » Blog Archive » Using PHP to update a database
Pingback: Tutorials » Archiwa bloga » Using PHP to update a database
Pingback: Using PHP to update a database - Learn Computer Tutorials
Pingback: learncomputertutorials
Pingback: Using PHP to update a database
Pingback: Computer Help Tutorials and Tips » Blog Archive » Using PHP to update a database
Pingback: programmingandcomputertutorials » Blog Archive » Using PHP to update a database
Pingback: Using PHP to update a database « Tutorials
Pingback: Using PHP to update a database « Learn Computer Tutorials
Pingback: Learn Computer Tutorials » Using PHP to update a database
Pingback: Balıkesir Üniversitesi Tutorials
Pingback: tutorials
Pingback: Using PHP to update a database « PHP Code
Pingback: PHP Code » Using PHP to update a database :: iBlog
Pingback: CW Connect — Blog — Using PHP to update a database
Pingback: Learn Computer Tutorials » Blog Archive » Using PHP to update a database
Pingback: Tutorials » Archiwa bloga » Using PHP to update a database
Pingback: Programming and Computer Tutorials - Using PHP to update a database
Pingback: tutorials › Using PHP to update a database
Pingback: phpcode » Using PHP to update a database
Pingback: Tutorials » Using PHP to update a database
Pingback: Using PHP to update a database « phpcode
Pingback: Using PHP to update a database « Computer Help Tutorials and Tips
Pingback: Using PHP to update a database « learncomputertutorials
Pingback: » Using PHP to update a database learncomputertutorials
Pingback: Booned — Blog — Using PHP to update a database
Pingback: Tutorials » Blog Archive » Using PHP to update a database
Pingback: Tutorials » Using PHP to update a database :: iBlog
Pingback: Programming and Computer Tutorials » Using PHP to update a database
Pingback: Using PHP to update a database « Programming and Computer Tutorials
Pingback: Computer Help Tutorials and Tips » Using PHP to update a database
Pingback: PHP Code » Using PHP to update a database
Pingback: PHP Code : Using PHP to update a database
Pingback: Using PHP to update a database @ PHP Code
Pingback: programmingandcomputertutorials
Pingback: » Using PHP to update a database
Pingback: Using PHP to update a database « computerhelptutorialsandtips
Pingback: Using PHP to update a database « PHP Code
Pingback: Using PHP to update a database « PHP Tutorials
Pingback: PHP Code » Blog Archive » Using PHP to update a database
Pingback: Using PHP to update a database – Tutorials
Pingback: sql update error using php
Pingback: sql update error using php | Jisku.com - Developers Network
Pingback: Arrays, Foreach, & Explode…Oh My!
Pingback: Populating Multi-Select Boxes From Your DB Using an All-in-One Form
Pingback: Populating Multi-Select Boxes From Your DB Using an All-in-One PHP Form
Pingback: Populating Multi-Select Boxes From Your DB Using an All-in-One PHP Form | clio 3 blog