FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Inserting data to MYSQL with a PHP script





albadil
Lets just recap on how our database will store the information we send to it. Here is the information we need to create our insert script.

# First Name
# Last Name
# The persons phone number
# Mobile Number
# The persons email address
# Country Of Residence

To insert this information you will need to use a mysql statement within a php script.

-------------------------------------------------------------------------------
$query = "INSERT INTO addressbook VALUES ('','John','doe','01204 444932','07976 695594','you@email.com','England')";
-------------------------------------------------------------------------------

to execute this line of sql then you would you following code below the $query.

-------------------------------------------------------------------------------
mysql_query($query);
-------------------------------------------------------------------------------

Now i’ll explain how the $query = line works. First of all we request to INSERT the following VALUES into the addressbook table.

John will be inserted into the first_name field.
doe will be inserted into the last_name field.
01204 444932 will be interested into phone_number field.
07976 695594 will be inserted into the mobile field.
you@email.com will be inserted into the email field.
England will be intested in to the country field.

Just a note: you can exclude the id field out of INTERT SQL as we have already instructed the field to auto_increment on every insert. So everytime we insert a record it will receive a UNIQUE identifier i.e. 1, 2 3 etc.. The next thing we need to do is create a HTML form that allows the user to post the informaton into the INSERT query mentioned above.

-------------------------------------------------------------------------------
< form action="add_contact.php" method="post">
First Name: < input type="text" name="first_name"/>
Last Name: < input type="text" name="last_name"/>
Phone: < input type="text" name="phone_number"/>
Mobile: < input type="text" name="mobile"/>
E-mail: < input type="text" name="email"/>
Country: < input type="text" name="country"/>
< input type="Submit"/>
< /form>
-------------------------------------------------------------------------------

Now create a page called add.html and insert the above html code into it. The form is pointed to add_contact.php this is the location to user submitted information will be sent.

create a new PHP file named add_contact.php and insert the code below into it.

-------------------------------------------------------------------------------
< ?
$user="your_username";
$pass="your_password";
$db="database";
$first_name=$_POST['first_name'];
$last_name =$_POST['last_name'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
$country=$_POST['country'];
mysql_connect(localhost,$user,$pass);
@mysql_select_db($db) or die( "Unable to select database, Please contact your administrator");
$query = "INSERT INTO addressbook VALUES ('','$first_name','$last_name','$phone','$mobile',
'$email','$country')";
mysql_query($query);
mysql_close();
?>
-------------------------------------------------------------------------------

Thats it for this installment

Good luck
Good bye
Sunny
Moved to Tutorial Sections,

Next time when you are copying and pasting stuff, do it in quote tags.
Related topics
Phoenix free PHP scripts
error in my PHP script
how can i make a php script for enter username and password?
Running a "Hello World" php script
PHP Script needed
Need php script 50 frih$ if you do
Free PHP/MySQL login script
PHP Script Needed for 250 frih$
PHP script to create email addresses/ftp accounts/etc...
php and xml
inserting php script within SPIP pages!!!
How do you Create a New Database using a php script?
[Help] php script to post to twitter
PHP script execution time
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.