PHP MySQL
Create & Test MySQL Database

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 Perfect HTML Editor

HTMLPad 2014Maybe PERFECT is a stretch, but I'm sold on HTMLPad 2014.

I've used it for 5 years and every upgrade has made it even better than before.

If you're working your way thru this tutor, you're probably pretty serious about your web development ventures.

HTMLPad is the best investment I have ever made when it comes to building and maintaining my web sites.

Find out how to get it for my Exclusive Price!

$28.76USD and it's ONLY HERE.

 

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 2014 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.

Copy & Save as: birthdays_insert_form.php

<html><head><title>Birthdays Insert Form</title>
<style type="text/css">
td {font-family: tahoma, arial, verdana; font-size: .875em }
</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>

Copy & Save as: 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>

 

What's Dbase Interface?

This is just a simple menu that makes navigating the scripts a bit easier.

Make sure you use the names for the scripts provided in the lessons.

Copy & Save as: birthdays_dbase_interface.php

<html>
<head>
<style type="text/css">
body {
max-width: 1200px;
margin: 0 auto;
font-family: arial, tahoma, serif;
font-size: 16px
}
div {
width: 250px;
margin: 0 auto;
padding: 20px;
background: #99ccff;
border: solid #000038 1px;
}
h1 {
text-align: center
}
</style>
</head>
<body>
<h1>DBase Interface</h1>
<div>
<?php
print "<a href=birthdays_insert_form.php>Enter Records</a><br>";
print "<a href=birthdays_display_records.php>Display Data</a><br>";
print "<a href=birthdays_update_form.php>Update a Record</a>";
?>
</div>
</body>
</html>

Continue to Displaying Records

You should complete all of the lessons, before you begin to modify the scripts.

MySQL Tutorial

To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com