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,960 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

MySQL – XAMPP

Posted Nov 30th, 2008 by Conor in in Linux, Mandriva

I ran into great difficulties getting MySQL to work with the Konsole on Mandriva. I stayed up untill about 5am last night trying to get it to work to no avail. Finally this evening I hit a break through. After trauling through forums last night I found no answers – just millions of people asking the same question so I hope to answer it here!

Method 1

This method is recommended all over the internet and i’m sure it does work – but with a great work around. I spent a lot of time trying to get this to work and I couldn’t so I reverted to method 2 below. I strongly recommend trying method 2 because it simply just works! If anyone can point out what I’m doing wrong here please let me know.

Assuming that you have urpmi installed type the following:

urpmi mysql

Blah Blah Blah and it is installed. Now lets enter mysql.

mysql -u root -p
Enter Password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

No! I have got the infamous error 2002! :-( I spent so long trying to fix this error and in the end I estabalished that the mysql.sock file was simply never created. I could go on and list all the solutions that I tried that don’t work but at this point I think the best thing for you to do at this point is to uninstall.

urpme mysql

Good all’s good and now lets revert to method number 2 which actually does work without much trouble.

Method 2

Now this method is easy and will take no longer than five minutes. We are going to install a package called XAMPP which contains Apache 2.2.9, MySQL 5.0.67, PHP 5.2.6 and phpmyadmin among others. It is very usefull. To download just type:

wget http://kent.dl.sourceforge.net/sourceforge/xampp/xampp-linux-1.5.5a.tar.gz

Then to install:

tar xvfz xampp-linux-1.5.5a.tar.gz -C /opt

It should now be installed – simple as that! To start XAMPP:

/opt/lampp/lampp start
Starting XAMPP 1.5.5a…
LAMPP: Starting Apache…
LAMPP: Starting MySQL…
LAMPP started.

Now that it is started type http://localhost in your browser and there you go!

Now you have XAMPP working which has phpmyadmin which you can use for mysql. If you would rather have MySQL in a CLI (like me) then you can simply follow the instructions below.

MySQL will not work initially because you need to locate the mysql.sock file. To gain immediate access just type:

mysql -u root --socket=/opt/lampp/var/mysql/mysql.sock

Brilliant its working ! This is only a temporary fix however because you don’t want to have to type in the socket location every time. To set the socket location permenantly to the new location do the following:

vim /etc/my.cnf/

This will open up the documet. Type “i” to get into insertion mode (to edit the text). There are two sections here that you need to edit – client and mysqld. They both have a socket location, if you replace both of those locations with the new one (/opt/lampp/var/mysql/mysql.sock) it should do the job. Press escape to exit insertion mode and press “:wq” to exit and save. Now your MySQL should work by just typing:

mysql -u root -p

Hopefully that works for you, it did for me. As I said earlier I couldn’t find any decent solution online. If you see any problems with what I have written just let me know!

4 responses so far



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