Conor Mac Aoidh
http://macaoidh.name
conor@macaoidh.name
 

Search

Archives

  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008

Spam

3,954 spam comments
blocked by
Akismet

Tag Cloud

    bt broadband CMS conor's management system conormacaoidh conor mac aoidh content management system drums fast Fedora fedora 10 furasta furasta cms furasta org gnome guide hello world HTML icsp iPhone jar Java javascript joomla kde Linux Mandriva mc kennas Monaghan Music mysql php Scratch stealing the ceiling The Dominican Affair the pot smoking pirates the strats tutorial Twitter updates web design forum webme webworks weekly tweets Windows wordpress

Ads

Collecting Data From Forms – PHP / MySQL

Posted Mar 20th, 2009 by Conor in in HTML, Languages, PHP

Today 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!

No responses yet

Mandriva :-)

Posted Nov 22nd, 2008 by Conor in in Linux, Mandriva, Web Projects, Windows

Yeaterday I met a guy browsing the Linux magazines in easons. We started talking about Linux and he highly recomended Mandriva. I had been using Fedora for a month at that stage and to be honest I didn’t think that much of it. The man managed to convince me to buy the Linux Format magazine which included a live version of Mandriva.

When I got home I stuck in the CD, not expecting much since I was disapointed with both Fedora and Ubuntu. Oh My God it is brilliant!! Mandriva is the best operating system that I have ever stumbled upon! Absolutly brilliant! :-) It combines all the good visual aspects of Microsoft’s Window Vista and the good old back end of Linux! The graphical ‘flowing’ widgets and the availability to completly customise everything visually creates a great user friendly (and more importandly Windows user friendly) interface.

So I set about getting rid of Fedora. I only had 12 GB hard drive space reserved for Fedora but I was so impressed by Mandriva that I managed to clear 34 GB for it! The OS is now installed and almost fully functional.

I have to say I am surprised that a Linux distribution has finally achieved what I have been waiting for. The things that I want in Linux are finally available, which are:

  • The graphical, ease of use, user friendly aspects of a Windows distribution. Mandriva in my opinion has achieved this, and to my great suprise it has out-done any Windows OS that I have ever used in this section in particular.
  • No Viruses. This has allways been available – but without the other points it means nothing.
  • The ability to code properly, I have learned the hard way that this isn’t possible on Windows.
  • Oh and everything is easy! Like MySQL for example, i found it impossible to set it up on Windows without using PHP MyAdmin.

Over all i think Mandriva is brilliant and I cant wait to get it working properly. The only one problem that I did find with it is that my 02 3G modem won’t work properly on it. I have found some articles to hack the config files, which I intent to do as soon as I find out how to log in as root!!! :-) oh it will all work eventually…

Just to note I got The Strats website up at http://www.thestrats.com please check it out and give me a review here or preferably at http://www.webdesignforum.com/5904-review-my-new-site.html#post22842

13 responses so far



Conor's Blog is powered by Wordpress | Template design by Conor Mac Aoidh