Layout Database: Add Form
- ID: PHP - 11
- Date Added: 2006-07-14
- Category: PHP
- Difficulty: Medium
- Author: Rei
Getting Started
In this tutorial, I am assuming you're starting completely from scratch, therefore, you only know that you are allowed MySQL databases, but they are still blank. So, please dump the following mySQL code:CREATE TABLE `layout` ( `id` INT(3) NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `creator` VARCHAR(255) NOT NULL, `size` VARCHAR(255) NOT NULL, `type` VARCHAR(255) NOT NULL, `date` date NOT NULL default '0000-00-00', `link` VARCHAR(255) NOT NULL, PRIMARY KEY (id)); ) TYPE=MyISAM AUTO_INCREMENT=1 ;Notice the values between the ``? They have to match up the name values in your form.
add.html Form
You will need to create a file calledadd.html. In this file, we will have our usual form elements, and such. Here's our code:<form action="insert.php" method="POST"> <table width="100%" border="0" colspan="2" cellspadding="0"> <tr><td>Title</td><td><input type="text" name="title" size="30"></td></tr> <tr><td>Creator</td><td><input type="text" name="creator" size="30"></td></tr> <tr><td>Size</td><td><input type="text" name="size" size="30"></td></tr> <tr><td>Type</td><td><SELECT name="type" size="1"> <OPTION value="Div Layer"> Div Layer <OPTION value="Table"> Table <OPTION value="IFrame"> IFrame <OPTION value="Popup"> Popup </SELECT> </td></tr> <tr><td>Date Added</td><td><input type="text" name="date" size="30"></td></tr> <tr><td>Download Link</td><td><input type="text" name="link" size="30"></td></tr> <tr><td> </td><td><input type="submit" name="submit" value="Submit!"> <input type="reset" name="reset" value="Clear!"></td></tr> </table>This code will give us the following:
| Title | |
| Creator | |
| Size | |
| Type | |
| Download Link | |
Syntax Explanations
The bolded part in the form code above are fairly self-explainatory. Why did I bold them? These "name"s MUST be exactly the same as your PhpMyAdmin code, in other words, the values between `` that we filled in earlier. Are you still with me this far?insert.php Form
We have another very important element in our form that is the key to inserting the data into our database. It's calledinsert.php. Create this file insert.php, and paste the following code:
<?php
// Fill in your database information here
$hostname='localhost';
$user=''; // Your MySQL username.
$pass=''; // Your MySQL password
$dbase='';// Your database name.
$connection = mysql_connect("$hostname" ,
"$user" , "$pass") or die ("Can't connect to MySQL");
$db = mysql_select_db($dbase , $connection)
or die ("Can't select database.");
// Important - Don't edit below this line
// Inserting data into the database
$q = "INSERT INTO `layout` (id, title, creator,
size, type, date, link) VALUES ('', '$title', '$creator',
'$size', '$type', 'now()', '$link');";
// If data wasn't inserted:
$rs = mysql_query($q) or die ("Could not execute
query : $q." . mysql_error());
// If data was
successfully
inserted:
if ($rs)
{
echo "Thank you! Data has sucessfully
been inserted into the database.
The following is the data inserted:
Title: $title
Creator: $creator
Size: $size
Type: $type
Date Added: $date
Download Link: $link
<a href=\"javascript: history.back(-2)\">Add Another?</a>";
?>
This page is very self explainatory. Basically, what it does is that it takes the data from the add.html that you filled in earlier, and insert it into the database. If the process was successfully done, it will output the information that you inserted, however, if not, the opposite will happen: data will not be inserted, and you will be told what was wrong with your code.« back · clear ¦ reload · forward »
Akatsuki Designs © Rei [2006-2008]. Site, layout, and content © Rei. All rights reserved. Valid CSS and HTML
Fight Spam! | Spot a typing error? | HostTracker
Fight Spam! | Spot a typing error? | HostTracker



