View Single Post
  #1 (permalink)  
Old Oct 21st, 2007, 10:46
Rakuli's Avatar
Rakuli Rakuli is offline
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
PHP & MYSQL -- Put it In -> Change it Around -> Pull it Out

Hello everyone, I do hope you are having a marvellous day.

I have been a member of Webforumz for a bit over a month now and it has become apparent that I am spending 50% of my time answering questions about.

 Adding data from PHP to a mySQL database
 Updating said data once it is in the database
 Pulling that data from the database to use later.

I have decided to create this topic to go into some basic detail about these issues as c010depunk pointed out there is no such resource on Webforumz.

The following three posts will deal with each of the above-mentioned topics and will be using the same data in each situation. In all cases we will be dealing with a (x)HTML form and a single table in a mySQL database.

THE DATABASE DETAILS

Firstly the details of the example database:

Host : ‘localhost’
User : ‘root’
Password : ‘guineapigsrule’
Database: ‘guineapigs’

Here is the information for the table called petGuineapigs.

Code: Select all
CREATE TABLE guineapigs`.`petGuineapigs` ( 
`PIG_ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`pigName` VARCHAR( 50 ) NOT NULL ,
`pigColour` VARCHAR( 20 ) NOT NULL ,
`pigAge` SMALLINT( 3 ) NOT NULL ,
`pigDescription` TEXT NOT NULL ,
INDEX ( `pigName` , `pigColour` , `pigAge` ) 
) ENGINE = MYISAM
It is beyond the scope of this post to explain what the above SQL means in intricate detail but it creates a table with the following columns:

PIG_ID – The unique id of each guinea pig. This will increment by one with each record inserted into the database using mySQL’s AUTO_INCREMENT feature.

pigName – guinea pig name. Max 50 characters

pigColour – guinea pig colour. Max 20 Characters

pigAge – guinea pig age. Max 999

pigDescription – guinea pig description. A text field for storing a guinea pig description.

You can create the table by running the query in phpMyAdmin or similar tool available on almost all hosting services and LAMP/WAMP home hosting setups.


THE (x)HTML FORM


PHP: Select all

<form name=”guineapigForm” action=”<?php echo $_SERVER[‘php_self’]; ?>” method=”post”>

<label for=”pigName”>Guinea Pig Name</label>
<input type=”text” name=”pigName” id=”pigName” maxlength=”50” size=”30” /><br />
<label for=”pigColour”>Guinea Pig Colour</label>
<select name=”pigColour” id=”pigColour”>
<option value=”green”>Green</option>
<option value=”pink”>Pink</option>
<option value=”black”>Black</option
<option value=”blue”>Blue</option>
<option value=”polkadotted”>Polka Dots</option>
</select><br />
<label name=”pigAge”>Guinea Pig Age</label>
<input type=”text” name=”pigAge” id=”pigAge” maxlength=”3” size=”3” /><br />
<label name=”pigDescription”>Guinea Pig Description</label><br />
<textarea name=”pigDecription” id=”pigDescription”></textarea><br />
<input type=”submit” name=”submitPig” value=”Save Your Pig!” />
</form>
The form is fairly basic – difference you will see in the form action. This will send the form to the current page when submitted.
All the PHP in the following posts will beplace *before* any HTML so it will look like

<?php
// PHP CODE HERE
?>
<html>
<head>....etc. ect.


Without further delay..... The first post will show how to add the details in the form as a new record to the database. There will be no text, please read the comments in the file to see what is happening in the file.
Reply With Quote