Form Submission with PHP and MySQL
Views Today: 4, Total Views: 10618, Submitted by: Aeriff on 10-07-2006
Note: For this tutorial, you will need some knowledge of HTML and how forms work, some basic knowledge of PHP, and also how to create MySQL databases and run a query on a database.
--------------------
Well heres my first tutorial for Tutorialdash.com, and it is on Form submission using PHP and MySQL. In this tutorial you will learn how to display the contents of a submitted form using PHP, and then enter them into a MySQL database.
First to begin with, you will want to create your form. It can either be a HTML or a PHP file, for this tutorial we will be using PHP. All it is going to be is a simple user details form where the user can enter his/her Name, Email and Website. So here is our first file, call it anything you like for this tutorial, we will be using form1.php (note the // In front of 'this is form1.php' denotes a comment and is not parsed by the script.) --
Code
// This is form1.php
<form name=details action=form2.php method=post>
<b>Please enter the following contact details below:</b>
<br><br><br>
<label>Name:</label> <input type=text name=name></input><br>
<label>Email:</label> <input type=text name=email></input><br>
<label>Website:</label> <input type=text name=website></input><br><br>
<input type=submit value=Submit></form>
Now, that is just your basic HTML form with all the input values assigned a name which will come into play soon.. Now, if you noticed in the previous code for form1.php, it submits the form to form2.php using the POST method. So we will be using the PHP variable, $_POST to retrieve our data and enter it into the database. So open up your MySQL databases and create a new database called information and run the following query:
Code
create table data(
id int(4) not null auto_increment,
name VARCHAR(30) not null,
email VARCHAR(30) not null,
website VARCHAR(30) not null,
primary key(id)
);
The above query creates a new table in the database information, which we created earlier
Moving on, seeing as the form in form1.php is submitting to form2.php, we are going to need a form2.php to enter the information into the database!
Code
// This is form2.php
<?
if(isset($_POST['submit']))
{
mysql_connect("localhost", "<MySQL USERNAME>", "<MySQL Password>");
mysql_select_db("information");
mysql_query("insert into data values('null', '$_POST[name]', '$_POST[email]', '$_POST[website]')") or die(mysql_error());
echo "The following information was entered into the database<br><br><br>";
echo "<b>Name:</b> $_POST[name]<br>";
echo "<b>Email:</b> $_POST[email]<br>";
echo "<b>Website:</b> $_POST[website]<br><br><br>";
echo "Thanks for taking the time to submit your information.";
mysql_close();
} else {
echo "Do not try to visit this page directly. Please use the <a href=form1.php>forms</a>";
}
?>
Note: You will need to make changes to the code where I have marked them using <>.
Now lets break it down..
In the above code, the first if() statement checks to see if the form was submitted via the Submit button on the forms, otherwise it skips to the else section of the code and echoes the 'Do not try to visit this page directly.'
In the if() statement, the first thing that happens is a connection is made to the mysql database on localhost, using <MySQL Username> and <MySQL Password>, if it is successful, it will select the table 'data' that we created earlier, otherwise the script will stop and echo out the MySQL Error. If the script does not encounter any errors and connects to the database and selects the table with no problems, it will then enter the information posted from the forms, which is grabbed using the $_POST variable. The $_POST variable will get the data that is submitted via the POST method with that id, eg: $_POST[email] would get the data that was sent from the form with the id 'email' in the tag as you can see back in form1.php. The 'null' value in the mysql_query to insert the data is in the 'id' field of the database. It will automatically assign an id to the entry, eg: 1, 2, 3 etc. Int his case, if it is the first entry which it is more than likely to be, it will be assigned ID #1. Once the data has been submitted, the PHP then echoes onto the page the data that was submitted and a little thankyou note. Then finally the MySQL connection is closed with the issue of the mysql_close(); function.
And there you have it, my first tutorial.. I apologise if I didn't cover everything or covered too much or was just too confusing.. if so, you can contact me at seradius[xATx]gmail[xDOTx]com
Thanks.
User Comments
If you want to comment on this tutorial you must first register or login.
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/cinedict/public_html/tutorialdash/tutorialview.php on line 287
Post A Comment
Fill out the following form to comment on this tutorial.



Syndicate
Nice tutorial. Thanks!