Low End Mac Reader Specials
TypeStyler For Mac OS X is Now Shipping! Download The Free Fully Functional 60 Day Tryout at www.typestyler.com
Don't install Parallels to play poker online! Poker Mac will show you how
to download and install a native Mac poker application such as Full
Tilt Poker Mac.
Compare products like desktop computers, apple laptops, apple macs, and LCD Monitors side by side! All the information and reviews to make the best purchasing decision for new mobile phones, sat nav systems, or MP3 players. The Ciao online shopping community makes searching products easy for you.
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 - " - 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("\"",""",$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("\"",""",$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("\"",""",$short) . "\"><br>Extra (Optional. Extra HTML code may be required.) :<br><input type=text name=extra size=40 maxlength=128 value=\"" . ereg_replace("\"",""",$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("\"",""",$array_most_recent[URL]) . "\"><input type=hidden name=text value=\"" . ereg_replace("\"",""",$array_most_recent[text]) . "\"><input type=hidden name=short value=\"" . ereg_replace("\"",""",$array_most_recent[short]) . "\"><input type=hidden name=extra value=\"" . ereg_replace("\"",""",$array_most_recent[extra]) . "\"><p>$array_most_recent[text] < <a href=$array_most_recent[URL]>$array_most_recent[URL]</a> > (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>© Brian Knight, 2002</center></h1></body></html>";
- ?>
Next time, we'll look at the PHP code
used to display our Deal of the Day.
Recent Online Tech Journal Columns
- Optimized Software Builds Bring Out the Best in Your Mac, 06.30. Applications compiled for your Mac's CPU can load more quickly and run faster than ones compiled for universal use.
- Low End Mac's Safe Sleep FAQ, 06.15. What is Safe Sleep mode? Which Macs support it? How can you enable or disable it? And more.
- The Original Macintosh, 01.12. An in-depth look at the original Macintosh and how it shaped future Macs.
- The Innovative Lisa, 01.08. Apple's Lisa and how it paved the way for the Macintosh.
- More in the Online Tech Journal index.
Links for the Day
- Mac of the Day: Mac IIfx, Mar. 1990 - This 'wicked fast' 40 MHz Mac trumped the 33 MHz DOS world.
- Group of the Day: Mac mini List is for anyone using or contemplating a Mac mini
- March 19 in LEM history: 90: Mac IIfx - 99: Fool me twice? - 01: Add FireWire, USB to older Macs - Time to replace your iMac? - 02: The Mac Challenge - Installing Linux on a low-end Mac - 03: Value of the Lombard PowerBook - Your portable should have WiFi - PowerBook 1400 upgrades - 04: The video iPod - 07: Troubleshooting an iMac - 08: Intel Mac mini value
- Support Low End Mac
Recent Content on Low End Mac
- Could iPad Replace the Mac?, Mac Sales Up in 2010, Avoiding Windows 7 'Whenever Possible', and More, Mac News Review, 03.19. Also why your next Mac may be an iPad, science blogger abandons Apple, the benefits of standing while working, and more.
- The Mobile System Stampede, Lithium Battery That Can't Explode, Affordable SSD Options, and More, The 'Book Review, 03.19. Also June 2007 MacBook Pro external display issue, laptop stands, 1 TB ultraportable hard drive, Mini DisplayPort/HDMI adapter, and more.
- CardBus WiFi, the Shiira Browser, Ridding the Web of Flash, and Macs vs. PCs, Charles W. Moore, Miscellaneous Ramblings, 03.18. Mac longevity, Shiira speed, ambidextrous Mac and Windows use, and how Flash benefits Apple.
- How to Zoom Your Browser for a More Readable Web, Steve Watkins, The Practical Mac, 03.18. Instructions for zooming text and pages in Safari, Firefox, Camino, and Opera.
- How Ad Blocking Hurts Your Favorite Websites, Charles W. Moore, Miscellaneous Ramblings, 03.18. Ad income keeps the Web free. Blocking online ads hurts your favorite websites.
- Taking Apart the 12" PowerBook, John Hatchett, Recycled Computing, 03.17. There are a lot of steps involved in disassembling a 12" PowerBook. Proceed with caution.
- More links in our archive.
Recent Deals
- Best Intel iMac Deals, 03.17. Used 17" from $600; 20" from $750; 24" from $825; refurb 21.5" nVidia, $999; new, $1,099; refurb Radeon, $1,299; new, $1,399; refurb 27" 3.06, $1,499; more.
- Best G5 iMac Deals, 03.17. 17" 2.0 GHz, $380; 1.9 GHz iSight, $479 shipped; 20" 1.8 GHz, $509 shipped; 2.1 GHz iSight, $549 shipped.
- Best Time Capsule Deals, 03.17. Close-out 500 GB, $140; new 1 TB, $279; used 2 TB simultaneous dual-band, $400; new, $455. Shipping included.
- Best iPad Deals, 03.16. 16 GB iPad, $499; 32 GB, $599; 64 GB, $699; 16 GB with 3G, $629; 32 GB 3G, $729; 64 GB 3G, $829. Free ground shipping.
- Best iPod classic Deals, 03.12. Used 20 GB, $119; 40 GB, $139; 60 GB, $159; 30 GB video, $129; 60 GB, $159; 80 GB, $169; refurb 120 GB, $189; new, $214; 160 GB, $228 shipped.
- Best G3 iBook and AirPort Card Deals, 03.12. 366 MHz 12" clamshell, $89; 466, $125; 500 white CD, $100; 600, $199; 800 Combo, $239; 14" 900, $225.
- Best Xserve Deals, 03.12. Used 1 GHz dual G4, $499; 2.0 dual G5, $599; 2.3, $749; refurb 2.26 4-core Nehalem, $2,499; new, $2,699; 8-core, $3,449; refurb 2.66, $4,299; new, $4,799; more.
- More deals in our archive.
About LEM | Support | Usage | Privacy | Contacts
Navigation
Used Mac Dealers
Apple History
Video Cards
Email Lists
Favorite Sites
MacSurfer
MacMinute
MacInTouch
MyAppleMenu
InfoMac
Macs Only!
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac
Museum
DealMac
DealsOnTheWeb
Mac2Sell
ramseeker
Mac Driver Museum
JAG's House
System
6 Heaven
System 7 Today
the pickle's Low-End
Mac FAQ
Abandonware
Petition
Mac vs. PC Info
Affiliates
The Apple
Store
Mac
Connection
B&H
MacMall
TechRestore
ExperCom
Crucial
Memory
batteries.com
MacMinute
MacInTouch
MyAppleMenu
InfoMac
Macs Only!
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac
Museum
DealMac
DealsOnTheWeb
Mac2Sell
ramseeker
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End
Mac FAQ
Abandonware
Petition
Mac vs. PC Info
Mac Connection
B&H
MacMall
TechRestore
ExperCom
Crucial Memory
batteries.com
