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

Search

Archives

  • July 2010
  • June 2010
  • April 2010
  • March 2010
  • 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

4,729 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 Java javascript joomla kde Linux Mandriva mc kennas Monaghan Music mysql php repository 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

Tagged!

Posted Jan 16th, 2009 by Conor in in CMS,CSS,Fedora,HTML,Languages,Linux,Mandriva,Me,Music,My Blog,PHP,School,The Dominican Affair,Web Projects,internet

I got tagged by Kae today. I had actually never herd of the thing before, I just don’t do enough blog reading I guess! Anyway it’s tough to list seven things that people don’t already know. I don’t have much to talk about so don’t be surprised if you know half of these things already!:-)

1. I was sixteen on 19th September > Virgo (What a sign – a fucking  mermaid!). I am still in fifth year at school which i absolutely detest. In fact the only thing that is keeping me in there is the promise of College. From my perspective that is heaven! Parties all the time – and when there are no parties I will be studying for tests by Programming! What more could you want? I should probably mention that I have been in an all Irish school for my whole life and that I talk Irish at home. Native language gotta be done. Oh and just for the record my name, Conor Mac Aoidh is pronounced “Mac í” or “Mac e”.

2. I play the Drums in two bands > The Pot Smokin’ Pirates and The Dominican Affair. Guess which one I enjoy more? I started the drums when I was 13. Before that I played the bodhran (What a load of shite). I went to drum lessons for about a year then I tought myself. When I finished teaching myself I moved on to teaching other people. At the moment I am practicing for the local Panto. I have done it three times before, it is good crac on the night. Gotta hate the music though! The Pot Smokin’ Pirates where the first band I joined, or should I say formed! They were a school band but soon enough I discovered that school and rock music don’t go so well together! We used to practice in the lead singers garage. One day his older brother asked me to join his band ‘The Kamikaze Potato Men’. Thats when I really got to grips with the drums – I was playing with a band three years older than me! Eventually ‘The Kamikaze Potato Men’ got rid of the lead singer, found a new one and formed The Dominican Affair!

3. I am Oifigeach na hAirgeadais for Ógra Shinn Féin Muineacháin. I don’t want to delve to deeply into this because once you start me I can’t stop! Basically I support and am a member of Sinn Féin. I am in favor of a democratic-socialist, anti-neo-libiralist, egalitarian 32 county united Ireland. Make Partition History! 26 + 6 = 1.

4. Oh what else… I am half Scotish, half Irish and was born in England. I used to collect Warhammer! God I have to include that because it was not a minor thing in my life. For those of you who are familiar with the way it works, I had a 3,000 point Empire Army! That is pretty massive for a ten year old in fairness. The strange thing is that I used them for display. I only ever played a couple of times and found the games to complicated. My main concentration was actually painting the things. I used to paint them like a machine. I also had millions of nobles and all that I couldn’t use because I didn’t have enough points to incorporate them! Come to think of it my first army was High Elves. By the end of my Empire collection I had gotten pretty good at painting the models. Then I started with Bretonians. I had just enough of them to play – two core units and a general. I recently got rid of almost everything in my collection. I kept only a few things. My two best painted pieces – ‘The Green Night’ and a Celtos model which I made a cool base for. Also I have an old White Dwarf from the 80′s signed by the Eavy Metal Team. I might Ebay it! I think that I have enough to make a core unit and general army at the moment!

5. PHP. This tagged thing is directed towards the language users so I had better talk about it. I started learning HTML at the end of 2007 as part of a school transition year course. I didn’t pay much attention to it at first. Then by chance I got work expierence with the local web design company, webworks last summer. That is were i learned CSS and began on PHP. Not to soon after that I got hooked on the language and I love it! I am currently writing Furasta CMS, which will be in beta of it’s first “stable” version by the end of next week, hopefully! I am a linux user and have been since November! I started out with Mandriva One 2009 and then recently moved on to Fedora 10. I think that Linux bears a big role in the future of computers in general.

6. I can fight. With a sword I mean! I trained for Medieval Historical Re-enacting for too many years. I did all the shows where you dress up and people stare at you and ask you if they can look at your sword. It’s not all it’s cracked up to be really. It’s like any other physical sport. You do the same thing over and over and over untill you can do it in your sleep! Then finally once you have done it for a few years you dump the wooden swords and go on to metal! I have to say the proper equipment is pretty cool. It’s quite tough to fight with armour actually because it weighs a ton. I learned how to fight with a sword. shield, staff, pike, bow and arrow and claymore. I haven’t really done anything with it in the last year but there is talk of it starting again so you never know!

7. This is tough… Em… I am an atheist. I believe that believing in God is a completely personal thing and that it should have nothing what-so-ever to do with the state and especially the schools! I don’t have a problem with anyone else that believes in god I just hate the fact that so much trouble has been caused in this world because of religion. I was not baptised and brought up to make my own decision – and I have! :-) It annoys me that my school gets massive “grants” from Catholic Associations and therefore it is a Catholic School. Not only that but the fact that I have to take time out of classes to go to mass four times a year. Basically I think that religion in schools should be eradicated, but the religion class should be kept. I think it is a good idea to discuss religions and to make people aware that there are much more religions out there than Catholicism and that they should make their own decision rather than blindly following the crowd.

Now onto the tagging. I am bound by the rules to tag seven other people. The problem is that I only know one PHP developer and that is the person who tagged me! So for now I will leave this part. But I will update it once I meet other enthusiasts, so I’m not breaking the rules!

Rules

  • Link your original tagger(s), and list these rules on your blog.
  • Share seven facts about yourself in the post – some random, some weird.
  • Tag seven people at the end of your post by leaving their names and the links to their blogs.
  • Let them know they’ve been tagged by leaving a comment on their blogs and/or Twitter
One response so far



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