Low End Mac's Online Tech Journal

Adding Data with Your Browser

Website Automation with PHP and MySQL, Part 8

Dan Knight - 2002.05.14

Yesterday we created an empty database for our Deal of the Day. Today we'll create a way to easily add and edit data using a Web browser.

My son Brian < http://brkn.net/> has been doing a lot with PHP and MySQL, so I've worked with him on this project. Frankly, he's doing most of the work and teaching me quite a bit.

Here's the PHP script Brian put together to allow entering new deals using a Web browser instead of PHP My Admin. We'll display the script in a mono typeface and comments in our default proportional font.

<?php
 
$db_server = "localhost";
$db_username = "id";
$db_password = "password";
$db_name = "database";
$connection = @mysql_connect($db_server,$db_username,$db_password);
mysql_select_db($db_name,$connection);
 
echo "<html><head><title>Add Deal</title></head><body><h1><center>Add a deal v2.0</center></h1>";

First up is an if/then construct. If you've just launched the script, the process is not yet true, so the whole first section is bypassed.

The ! means "if this field is blank," so !$url means, "if there is no value for $url," then do whatever follows in curly brackets. The die command stops any following PHP commands through the end of the page. It's used here to check that each necessary field is not empty; if a required field is blank, this script will not write the record to our database.

if ($process == "true")
{
   if (!$url) { die("Enter the url."); }
   if (!$text) { die("Enter your text."); }
   if (!$short) { die("Enter short text."); }
   if (!$year) { die("Set a year."); }
   if (!$month) { die("Set a month."); }
   if (!$day) { die("Set a day."); }
   if (checkdate($month, $day, $year) == 0) { die("Choose a valid date."); }

We made an interesting discovery when we added the checkdate function to verify that the date added is a valid one. According to PHP.net, the function returns the value true for a valid date, false for an invalid date. The PHP 4 Bible only says it ensures validity without telling exactly what kind of result it returns. But when we tested for false, it didn't work like it was supposed to.

The Visual book on PHP gave us the clue we needed, indicating that the checkdate function returns an integer, not a text string. Testing for zero solved the problem - and I posted a note to PHP.net to alert others to the error in their online documentation.

   $sql_date = "$year$month$day";
   mysql_query("INSERT INTO `deals` (`ID`, `URL`, `text`, `short`, `extra`, `date`) VALUES ('', '$url', '$text', '$short', '$extra', '$sql_date')") or die(mysql_error());

The above line adds the deal to our database, and the next line tells us everything has worked.

   echo "<p>Deal added successfully. Would you like to add another?</p><hr width=75%>";
   $url = stripslashes($url);
   $text = stripslashes($text);
   $short = stripslashes($short);
   $extra = stripslashes($extra);
}

The stripslashes command removes the backslashes used to "escape" characters such as quote marks and parentheses within your field.

Now we come to the actual data entry. We begin by defining $cur_year as this year and $cur_month as this month. Since we'll generally be adding deals for this month and year, this will speed up data entry.

$cur_year = date(Y);
$cur_month = date(m);

The next line creates a standard HTML form that we can use in our browser. $PHP_SELF refers to this script. The ereg_replace command replaces a double quote with its HTML equivalent - &#34; - because HTML can't handle quote marks nested within quote marks.

This whole paragraph of code (I would have gone for easier to follow formatting, but Brian wrote the program) puts the form on the page and gives us a place to enter our data. The maxlength command beeps when you reach a field's maximum length and prevents you from typing in more characters than specified.

echo "<form action=$PHP_SELF method=post><input type=hidden name=process value=true><p align=center>URL (Your link will point here) :<br><input type=text name=url value=\"" . ereg_replace("\"","&#34;",$url) . "\" size=40 maxlength=128><br>Text (This will link to the URL specified above. Max. 64 characters) :<br><input type=text name=text size=40 maxlength=64 value=\"" . ereg_replace("\"","&#34;",$text) . "\"><br>Short link (This links to the same URL as above. Max. 32 characters) :<br><input type=text name=short size=40 maxlength=32 value=\"" . ereg_replace("\"","&#34;",$short) . "\"><br>Extra (Optional. Extra HTML code may be required.) :<br><input type=text name=extra size=40 maxlength=128 value=\"" . ereg_replace("\"","&#34;",$extra) . "\"><br><br>Date (2-digit month(01-12), 2-digit day (01-31), year) :<br><input type=text name=month size=4 maxlength=2 value=\"$cur_month\"> <input type=text name=day size=4 maxlength=2> <input type=text name=year size=6 maxlength=4 value=\"$cur_year\"><br><input type=submit name=submit value=Submit></p></form><p align=center>Duplicate a deal</p><table cellpadding=0 cellspacing=0 bgcolor=#ffffff width=100%>";

The next section of code lists the 20 deals with the latest scheduled dates of appearance. Deals are sorted in descending order by data, and you also have the option to duplicated an existing deal but give it a new date.

$most_recent = mysql_query("SELECT * FROM deals ORDER BY date DESC limit 20");
while ($array_most_recent = mysql_fetch_array($most_recent))
{
   echo "<tr$alt_color><td><form action=$PHP_SELF method=post><input type=hidden name=process value=true><input type=hidden name=url value=\"" . ereg_replace("\"","&#34;",$array_most_recent[URL]) . "\"><input type=hidden name=text value=\"" . ereg_replace("\"","&#34;",$array_most_recent[text]) . "\"><input type=hidden name=short value=\"" . ereg_replace("\"","&#34;",$array_most_recent[short]) . "\"><input type=hidden name=extra value=\"" . ereg_replace("\"","&#34;",$array_most_recent[extra]) . "\"><p>$array_most_recent[text] &lt; <a href=$array_most_recent[URL]>$array_most_recent[URL]</a> &gt; (original displays $array_most_recent[date])<br>Display on (mm/dd/yyyy) <input type=text name=month size=4 maxlength=2 value=\"$cur_month\"> <input type=text name=day size=4 maxlength=2> <input type=text name=year size=6 maxlength=4 value=\"$cur_year\"><input type=submit name=submit value=Duplicate></p></form></td></tr>";
   if ($alt_color == "") { $alt_color = " bgcolor=#eeeeee"; }
   else { $alt_color = ""; }
}

The $alt_color alternates the background color behind the deals - one white, one light gray, and so forth. It's a nice touch.

And always be sure to copyright your code. ;-)

echo "</table><h1><center>&copy; Brian Knight, 2002</center></h1></body></html>";
 
?>

Next time, we'll look at the PHP code used to display our Deal of the Day.

Join us on Facebook, follow us on Twitter or Google+, or subscribe to our RSS news feed

Today's Links

Recent Content

About LEM Support Usage Privacy Contact

Custom Search

Follow Low End Mac on Twitter
Join Low End Mac on Facebook

Favorite Sites

MacSurfer
Cult of Mac
Shrine of Apple
MacInTouch
MyAppleMenu
InfoMac
The Mac Observer
Accelerate Your Mac
RetroMacCast
The Vintage Mac Museum
Deal Brothers
DealMac
Mac2Sell
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End Mac FAQ

Affiliates

Amazon.com
The iTunes Store
PC Connection Express
Macgo Blu-ray Player
Parallels Desktop for Mac
eBay

Low End Mac's Amazon.com store

Advertise

Open Link