PHP MySQL
Interactive Website Design
Inserting Data into a Table
When we say Insert data, we are talking about adding the actual information in the fields of the table.
For example, an insertion in our table would be a first name, last name and the birth date of a user.
The basic SQL syntax is placed within the mysql query function statement.
mysql_query ();
Note: If you have accessed this web page via a search engine, you should go back and start on our home page. This tutorial is designed to be viewed and executed in sequence. Learn to build your database right on your PC and Export it to your website.
The SQL syntax is enclosed in double quotes. It supplies the command , the name of the table, the field names and the values to be added.
"INSERT INTO birthdays (firstname, lastname, birthday) VALUES ('Peggy', 'Donahue' 'June 4, 1956')"
The mysql_query function can be used in many other database operations.
The code for adding data to a table is shown below.
The first example (red) shows the actual VALUES which are added.
The second (blue) shows how to add data parsed from a simple form.
<?
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$birthday=$_POST['birthday'];
$db="newdb";
$link = mysql_connect("localhost");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
mysql_query ("INSERT INTO birthdays (firstname, lastname, birthday) VALUES ('Peggy', 'Donahue', 'June 4, 1956')");
mysql_query ("INSERT INTO birthdays (firstname, lastname, birthday) VALUES ('$firstname', '$lastname','$birthday')");
mysql_close($link);
?>
IndigoAMPP Users
We're going to add information to our fields using two scripts.
The first script is a form and the second script will process the form and add the data to the fields in our table.
Copy the code from this page and save the scripts with the supplied filenames.
If you downloaded the zip file, you can run the scripts by loading the birthdays_insert_form.php script and running it or you can access the script from the db interface script.
If you are copying and pasting, create both scripts before you run the form script.
HTMLPad 2010 users should know how to run a PHP script from the editor by now.
Load and run the birthdays_insert_form.php script. This script will call the birthdays_insert_record.php script which processes the information from the form.
Add a few records.
birthdays_insert_form.php
<html><head><title>Birthdays Insert Form</title>
<style type="text/css">
td {font-family: tahoma, arial, verdana; font-size: 10pt }
</style>
</head>
<body>
<table width="300" cellpadding="5" cellspacing="0" border="2">
<tr align="center" valign="top">
<td align="left" colspan="1" rowspan="1" bgcolor="64b1ff">
<h3>Insert Record</h3>
<form method="POST" action="birthdays_insert_record.php">
<?
print "Enter Firstname: <input type=text name=firstname size=30><br>\n";
print "Enter Lastname: <input type=text name=lastname size=30><br>\n";
print "Enter Birthday: <input type=text name=birthday size=20><br>\n";
print "<br>\n";
print "<input type=submit value=Submit><input type=reset>\n";
?>
</form>
</td></tr></table>
</body>
</html>
birthdays_insert_record.php
<html><head><title>Birthdays Insert Record</title></head>
<body>
<?
/* Change db and connect values if using online */
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$birthday=$_POST['birthday'];
$db="newdb";
$link = mysql_connect('localhost', 'root' , '');
if (! $link) die(mysql_error());
mysql_select_db($db , $link) or die("Select Error: ".mysql_error());
$result=mysql_query(
"INSERT INTO birthdays (
firstname,
lastname,
birthday) VALUES (
'$firstname',
'$lastname',
'$birthday')") or die("Insert Error: ".mysql_error());
mysql_close($link);
print "Record added\n";
?>
<form method="POST" action="birthdays_insert_form.php">
<input type="submit" value="Insert Another Record">
</form>
<br>
<form method="POST" action="birthdays_dbase_interface.php">
<input type="submit" value="Dbase Interface">
</form>
</body>
</html>
Exporting the Table
If you really want to increase your learning curve, I recommend that you take your first crack at exporting the birthdays table now. If you don't have a web host yet, you may just want to practice the Export part. If you already have a web host you can run through the entire process of exporting and then Importing it to your web hosting account. The more you practice the operation, the more comfortable you'll feel when it comes time to move your completed project online. Go to Export Table
If you don't feel you are ready, continue to Displaying Records
Please Note
If you have completed the lessons in order, all of the scripts in the birthdays_db download package are active. They can be run from the birthdays_dbase_interface.php script, with the exception of the birthdays_add_fields.php script. Don't run it until you've completed all the lessons.
You should complete all of the lessons, before you begin to modify the scripts.
Building Your Database on Your PC
In order to build your database on your PC and later Export it to your website requires setting up a localhost server on your PC and finding an HTML editor that will sync up with the server.
If you use Windows, the process of setting up and running a localhost so that you can test and edit scripts and build databases is very easy.
I recommend the IndigoAMPP web server and the HTMLPad 2010 HTML editor.
I recently installed IndigoAMPP for Windows on my Vista system and it ran on the first try after installation. Setting up HTMLPad 2010 to work with it is also an easy process.
I created a little tutorial that shows the whole process including how to start the server after you install it. Go to Tutorial
Note: The server is FREE and you can try the HTML editor for 30 sessions or 30 days free. If you can't learn to build your database in that amount of time, don't buy the editor. NO RISK!!
Download the Scripts
The Birthdays Database management files can be downloaded in a zip file.
If using IndigoAMPP download to c:\indigoampp\apache-2.2.11\htdocs.
Extract there and you'll have a birthdays folder inside your htdocs folder. Run the scripts from there.
The package contains an integrated db management system, with a simple interface.
This Instruction file is included in the download.
Download birthdays_db.zip
MySQL Tutorial
To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com
Contents
Introduction to PHP MySQL
Connecting to the Server
Creating a Database
Create a Table
Displaying Records
Editing Records
Delete Records
Altering Tables
Export Table
Advanced
Multiple Selection Forms
Create a Simple Poll
Need Web Hosting?
We Recommend these products
Free Flash Websites
