The script you'll create for a record addition is a lot simpler than the script for table creation!
Open a new file in your text editor and start a PHP block:
<?
Check that values were actually entered for $_POST[id], $_POST[format] , and $_POST[title]. If they weren't, direct the user back to the form and exit the script:
if ((!$_POST[id]) || (!$_POST[format]) || (!$_POST[title])) {
header("Location: /show_addrecord.html");
exit;
}
| Note |
You can have as many (or as few) required fields as you want. |
Create a variable to hold the name of the database on which the table resides:
$db_name = "testDB";
Create a variable to hold the name of the table you're populating with this script:
$table_name = "my_music";
Add the connection information just as you have been doing:
$connection = @mysql_connect("localhost", "spike", "9sj7En4")
or die(mysql_error());
Select the database as you have learned to do:
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
Create the SQL statement. The first parenthetical statement gives the names of the fields to populate (in order), and the second parenthetical statement sends the actual strings:
$sql = "INSERT INTO $table_name
(id, format, title, artist_fn, artist_ln, rec_label,
my_notes, date_acq) VALUES
('$_POST[id]', '$_POST[format]', '$_POST[title]',
'$_POST[artist_fn]', '$_POST[artist_ln]', '$_POST[rec_label]',
'$_POST[my_notes]','$_POST[date_acq]')";
Create a variable to hold the result of the mysql_query() function, as you have learned to do:
Close your PHP block and add HTML:
?> <HTML> <HEAD> <TITLE>Add a Record</TITLE> </HEAD> <BODY>
Add a nice heading so that the users know what they are viewing. Mingle HTML and PHP to include the value of the $table_name variable:
<H1>Adding a Record to <? echo "$table_name"; ?></H1>
Next, you'll re-create the layout used in show_addrecord.html, only it won't contain form fields. Instead, you'll mingle HTML and PHP to show the values that were entered. Start a new table row and table data cell, and then display a text label and value for ID:
<TABLE CELLSPACING=3 CELLPADDING=3> <TR> <TD VALIGN=TOP> <P><STRONG>ID:</STRONG><BR> <? echo "$_POST[id]"; ?></P>
Display a text label and value for the date acquired, and then close the table data cell:
<P><STRONG>Date Acquired (YYYY-MM-DD):</STRONG><BR> <? echo "$_POST[date_acq]"; ?></P> </TD>
Display a text label and the format of the recording, and then close the table data cell and table row:
Start a new table row and table data cell, display a text label and value for the title, and close the table data cell:
<TR> <TD VALIGN=TOP> <P><STRONG>Title:</STRONG><BR> <? echo "$_POST[title]"; ?></P> </TD>
In a new table data cell, display a text label and value for the record label information, and then close the table data cell and table row:
<TD VALIGN=TOP> <P><STRONG>Record Label:</STRONG><BR> <? echo "$_POST[rec_label]"; ?></P> </TD> </TR>
Start a new table row and table data cell, and then create an input field for the artist's first name with a text label. Close the table data cell after the input field:
<TR> <TD VALIGN=TOP> <P><STRONG>Artist's First Name:</STRONG><BR> <? echo "$_POST[artist_fn]"; ?></P> </TD>
In a new table data cell, display a text label and value for the artist's last name (or group name), and then close the table data cell and table row:
<TD VALIGN=TOP> <P><STRONG>Artist's Last Name (or Group Name):</STRONG><BR> <? echo "$_POST[artist_ln]"; ?></P> </TD> </TR>
Start a new table row and a table data cell that spans two columns. Display a text label and value for your notes regarding the recording:
<TR> <TD VALIGN=TOP COLSPAN=2 ALIGN=CENTER> <P><STRONG>My Notes:</STRONG><BR> <? echo stripslashes($_POST[my_notes]); ?></P>
| Note |
The stripslashes() function will remove any slashes automatically added to your form data, which is turned on by default in PHP. It will add slashes where necessary to escape special characters, such as single quotes and double quotes. You can turn it off by modifying your php.ini file, but if you leave it on, it's one less thing you have to worry about. |
Add a link back to the original form, and then close the table data cell, the table row, and the table itself:
<P><a href="show_addrecord.html">Add Another</a></P> </TD> </TR> </TABLE>
Add some more HTML so that the document is valid:
</BODY> </HTML>
Save the file with the name do_addrecord.php, and place it in the document root of your web server.
Your code should look something like this:
<?
//check for required fields
if ((!$_POST[id]) || (!$_POST[format]) || (!$_POST[title])) {
header("Location: /show_addrecord.html");
exit;
}
//set up database and table names
$db_name = "testDB";
$table_name = "my_music";
//connect to MySQL and select database to use
$connection = @mysql_connect("localhost", "spike", "9sj7En4")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
//create SQL statement and issue query
$sql = "INSERT INTO $table_name
(id, format, title, artist_fn, artist_ln, rec_label,
my_notes, date_acq) VALUES
('$_POST[id]', '$_POST[format]', '$_POST[title]',
'$_POST[artist_fn]', '$_POST[artist_ln]', '$_POST[rec_label]',
'$_POST[my_notes]','$_POST[date_acq]')";
$result = @mysql_query($sql,$connection) or die(mysql_error());
?>
<HTML>
<HEAD>
<TITLE>Add a Record</TITLE>
</HEAD>
<BODY>
<H1>Adding a Record to <? echo "$table_name"; ?></H1>
<TABLE CELLSPACING=3 CELLPADDING=3>
<TR>
<TD VALIGN=TOP>
<P><STRONG>ID:</STRONG><BR>
<? echo "$_POST[id]"; ?></P>
<P><STRONG>Date Acquired (YYYY-MM-DD):</STRONG><BR>
<? echo "$_POST[date_acq]"; ?></P>
</TD>
<TD VALIGN=TOP>
<P><STRONG>Format:</STRONG><BR>
<? echo "$_POST[format]"; ?>
</P>
</TD>
</TR>
<TR>
<TD VALIGN=TOP>
<P><STRONG>Title:</STRONG><BR>
<? echo "$_POST[title]"; ?></P>
</TD>
<TD VALIGN=TOP>
<P><STRONG>Record Label:</STRONG><BR>
<? echo "$_POST[rec_label]"; ?></P>
</TD>
</TR>
<TR>
<TD VALIGN=TOP>
<P><STRONG>Artist's First Name:</STRONG><BR>
<? echo "$_POST[artist_fn]"; ?></P>
</TD>
<TD VALIGN=TOP>
<P><STRONG>Artist's Last Name (or Group Name):</STRONG><BR>
<? echo "$_POST[artist_ln]"; ?></P>
</TD>
</TR>
<TR>
<TD VALIGN=TOP COLSPAN=2 ALIGN=CENTER>
<P><STRONG>My Notes:</STRONG><BR>
<? echo stripslashes($_POST[my_notes]); ?></P>
<P><a href="show_addrecord.html">Add Another</a></P>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
Go on to the next step, where you get to click a button and add a record.