|
|
#1 |
|
Gold Member
![]() Join Date: Oct 2008
Location: Ireland
Posts: 349
|
Hi,
There have been quite a lot of requests recently on collecting data from forms using PHP and MySQL so I decided to sort them all out in a small tutorial thread. Here goes nothing! 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: Code:
create database form; Code:
use form; Code:
create table users ( user_id int not null auto_increment primary key, username text, email text, password char(40) default null ); Code:
<?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');
?>
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. Code:
<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> Code:
<?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;
?>
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. I should mention that this tutorial was taken from my blog post on the subject located here. Any comments or feedback are welcome.
__________________
Conor |
|
|
|
![]() |
| Tags |
| collecting, form, information, mysql, php |
| Thread Tools | |
| Display Modes | |
|
|