Collecting Data From Forms – PHP / MySQL
Posted Mar 20th, 2009 by Conor in in HTML, Languages, PHPToday i had quite a few requests for the same thing on the Web Design Forum. So instead of writing a different answer to all of the threads I decided to summarise it all in a blog post on Collecting Data From Forms Using PHP and MySQL. Hopefully I will just be able to link back to this post when I get this question again!
What is PHP? PHP is a programing language with an easy to use syntax. It resembles C and Java. PHP is excecuted server side which means that if you want to do something with the language then you will have to send a request to the server. This is usually done by pointing to a PHP file to proccess information. It can also be done using AJAX without requiring a reload – but that’s a whole other ball game!
What is MySQL? MySQL is one of many Structured Query Languages. It is basically a language used to store and manipulate data through the use of databases. I have often compared it to Microsoft’s Excel – many people tell me that I’m wrong with that comparison but I think that it is valid!
PHP + MySQL = Dynamic Webpage. In my opinion MySQL and PHP have one common characteristic; they both have a user friendly syntax which is easy to learn and yet they both have the ability to unleash massive amounts of power once you delve into them! The combination of the two languages produces a constantly changing, dynamic website. For example you could have page content being called from the database and then to update that content all you need to do is update the database – rather than trawling through code.
If you are finding this all hard to follow don’t worry, I’m sure it will be a bit more self-explanatory when we look at some code. I am not going to go into the detail of downloading and installing PHP and MySQL. The following assumes that you have done so yourself – or that you are working on an online server with these packages pre-installed.
Setting Up MySQL
First you will need to create a new database and a new table for all of the information to be stored in. Enter this command to create the database:
create database form;
Quickly select that database – tell MySQL that you want to use it.
use form;
Then you will need to create a table which will store our information.
create table users (
user_id int not null auto_increment primary key,
username text,
email text,
password char(40) default null
);
Now that you have the MySQL side of things running you will need to connect to MySQL with PHP. The file below should be named ‘connect.php’. /*These things are comments */
<?php
/* The name of the database that we have just created */
$dbname = 'form';
/* The hostname which will usually be localhost but change if appropriate */
$hostname = 'localhost';
/* Database Username and Password - you must change these to your own values*/
$dbuser = 'root';
$dbpass = '';
$connect = mysql_connect($hostname,$dbuser,$dbpass) or die('Could not connect to MySQL please insure that the connection information is correct');
mysql_select_db($dbname,$connect) or die('Could not select the database');
?>
Writing The Form
Now that we have written our connection information with PHP we must include that file in our main PHP file and write a form. This is a normal HTML form which collects information from users wishing to register to whatever.
<form method="post">
Username: <input type="text" name="username"/><br/>
Email: <input type="text" name="email"/><br/>
Password: <input type="password" name="password"/><br/>
Confirm: <input type="password" name="confirmpass"/><br/>
<input type="submit" name="save" value="Register"/>
</form>
That is alright as it is but it doesn’t really do anything. If we want to collect that information then we must work our PHP magic! This file contains the same HTML form but it is written in PHP and is named ‘form.php’.
<?php
/* Include the connection file that we created earlier */
require_once 'connect.php';
/* If the user presses the submit save the information to the database and display a thank you message */
if(isset($_POST['save'])){
/* Get the values submitted from the form */
$username = addslashes($_POST['username']);
$email = addslashes($_POST['email']);
$pass = $_POST['password'];
$password = sha1(strip_tags($pass));
/* Check to see if the two password values are correct */
if($pass!=$_POST['confirmpass'] || $pass=='') die('Passwords do not match. <a href="form.php">Return to form.</a>');
/* Update the database with the new information or die with the mysql error */
mysql_query("insert into users values ('','$username','$email','$password')") or die(mysql_error());
/* Display thank you message and exit */
echo '
<h1>',$username,' Thank You For Registering!</h1>
';
exit;
}
/* Else display the form so the user can register */
echo '
<form method="post">
Username: <input type="text" name="username"/><br/>
Email: <input type="text" name="email"/><br/>
Password: <input type="password" name="password"/><br/>
Confirm: <input type="password" name="confirmpass"/><br/>
<input type="submit" name="save" value="Register"/>
</form>
';
/* Close MySQL, exit and close PHP */
mysql_close();
exit;
?>
Woah! Thank god that’s over. If all goes well you will not have any errors running that script. Anyway that’s all the advice I can give you for one day. Good luck!
Please feel free to use or adapt this code as you will. I understand that this isn’t a prime example but it sets the foundation of what you can customise in order to create a brilliant user freindly form! You could also try adding more verification to the form or even using some unobstructive JavaScript to make it look nice. These are all things that I might write a tutorial about in the future but that’s it for now!