Adding Data with Your Browser
Website Automation with PHP and MySQL, Part 8
Low End Mac Reader Specials
Memory To Go Special: New 2008 iMac 2GB $42 / iMac Intel Core2 DUO & MacBook Pro 2GB $36 - 1GB $20. MacPro 8 Core Memory 8GB kit $286 / 4GB kit $143 / 2GB kit $93 -- Free shipping available. LIfetime warranty.
Download Typestyler, still the Ultimate Styling Tool for Internet, Print and Video Graphics. Works great in Classic with a Native OS X Version on the way. Free Tryout: www.typestyler.com
LA Computer Company: Specials on AppleCare, iMac's, Apple Batteries and Apple A/C Adapters. Also Great prices on Used Apple Computers. Call 1-800-941-7654 Click Here.
OWC: Mercury Elite FW800/FW400/USB2/eSATA up to 2.0TB TOP-RATED Solutions offer High Performance, Reliable storage for all your data storage needs. 500GB $159.99, 750GB $199.99, 1.0TB from $299.99
Mac users can finally play Party Poker for Mac. Not only that, they can also learn how to play PokerStars for Mac.
Laptop Hardware Provided by TechRestore - Overnight Mac & iPod Repairs.
Compare products like desktop computers, laptops, and LCD TVs side by side! All the information and reviews to make the best purchasing decision for a new cell phone GPS products or MP3 players. The Ciao network makes searching products easy for you.
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
- Apple's AAUI ethernet connector, 09.04. From 1991 through 1995, Apple used a proprietary ethernet connection. Why they created AAUI and where to find adapters.
- PowerPC G5: Apple's last fling with PowerPC architecture, 05.24. Teaming up with IBM, Apple adopted the PowerPC G5 in 2003 - and phased out the last G5 Power Mac three years later.
- The PowerPC G4: From 350 MHz to 2.0 GHz, 05.24. AltiVec and dual processor support made the G4 a big improvement over the earlier G3 processor.
- The PowerPC G3 story: From 233 MHz to 1.1 GHz, 05.24. The history of the PowerPC 750 family and its use in Apple computers from 1997 through 2004.
- More in the Online Tech Journal index.
Links for the Day
- Mac of the Day: Motorola StarMax 5000, May 1997 - This second-generation Mac clone offered 603e, 604e processors.
- List of the Day: The iPod List The iPod List is a forum to discuss the iPod, it's accessories, the iTunes Store, iTunes, and related topics.
- October 13 in LEM history: 98: Evidence that Macs last longer - 99: A Mac is like Prozac - From home computers to a real computer - 00: Tradeoffs for OS X beta - 03: iBook failures - 05: The 2005 iMac G5 value equation - Email on your iPod - OS X on 4 dual-core CPUs - 06: The legendary Apple Extended Keyboard - Stinky old iBook smells like sweat - Apple's climb back to success
Recent Content on Low End Mac
- TruePower Battery Can Run WallStreet PowerBook Past the 5 Hour Mark, Tommy Thomas, Welcome to Macintosh, 10.10. If you have a rugged old PowerBook but its battery is losing capacity, TruePower can give you plenty of time in the field.
- nVidia Inside Next MacBook?, Time for a Mac Netbook, Asus Launched MacBook Air Killer, and More, The 'Book Review, 10.10. Also photo reveals more about MacBook Pro, comparing 16:9 and 16:10 displays, Apple settles suit over faulty iBook and PowerBook adapters, bargain 'Books from $150 to $2,699, and more.
- 30% of iPhone 3G Buyers Switched Carriers, EU Battery Rule May Force iPhone Redesign, and More, iNews Review, 10.10. Also iPhone 3G greatest consumer electronics device ever, track presidential polls on your iPhone, Talking English Dictionary, waterproof armbands, several new iPhone apps, and more.
- Economic Crunch May Slow Mac Sales, a Recycled Cube, ToCA Race Driver 3 for Mac, and More, Mac News Review, 10.10. Also don't buy RAM from Apple, customize your Mac's appearance, MacTribe expanding into print, My Apple Space social networking, and more.
- Best Mac Pro Deals, Low End Mac Deals, 10.10. Used 2.66 GHz 4-core, $1,799; new, $1,949 after rebate; 2.8 4-core, $2,099 shipped; 8-core, $2,599 shipped; 3.0 $3,399 shipped; 3.2, $4,099 shipped.
- Best PowerBook G3 Deals, Low End Mac Deals, 10.10. Used 14" WallStreet G3/266 MHz, $90; Lombard G3/400 MHz, $150; Pismo G3/400 MHz, $300; 500 MHz, $350.
- Best Time Capsule and AirPort Deals, Low End Mac Deals, 10.10. Refurb 500 GB Time Capsule, $249; new, $294; refurb 1 TB, $419; new, $462; AirPort Extreme Card, $39; Base Station, $159; Express, $60.
- Modding Your Old Mac to Make It More Useful, Phil Herlihy, The Usefulness Equation, 10.09. If your old Mac is too slow, too noisy, too plain looking, or has too little room for expansion, you might want to mod it.
- What Would an $800 MacBook Mean for the Mac mini?, Dan Knight, Mac Musings, 10.09. If Apple does release an $800 entry-level MacBook next week, the $600 Mac mini is going to look very overpriced.
- Best iMac G4 Deals, Low End Mac Deals, 10.09. Used 15" 700 MHz CD-RW, $269; 800 Combo, $300; 1 GHz, $390; 17" 1.25 GHz SuperDrive, $400; 20", $529.
- Best 15" MacBook Pro Deals, Low End Mac Deals, 10.09. Used 1.83 GHz Core Duo, $995; 2.16, $1,125; new, 2.2, $1,400 after rebate; refurb 2.4, $1,699; 2.5, $1,999; 2.6, $2,299; rebates on new.
- Best Mac OS X 10.4 'Tiger' Deals, Low End Mac Deals, 10.09. DVD upgrade from 10.3, $75; upgrade bundle with 10.3, $118; full version, $129; family pack, $200; 10-user Server, $350; unlimited, $400.
- The Power of Older Macs, Why Vista Only Sees 3 GB of RAM, Wangwriter Supplies, and More, Charles W. Moore, Miscellaneous Ramblings, 10.08. Also the end of an era as MIT HyperArchive shuts down and another suggestion for profiling Windows computers.
- Migrating My Law Office from Windows to Macintosh, Andrew J Fishkin, Best Tools for the Job, 10.08. By switching to Leopard Server, everyone in the office will be able to move to a Mac - but which ones will best meet their needs?
- Low End Mac Needs Help Moving to Joomla, Dan Knight, Mac Musings, 10.08. We've settled on Joomla as the content management system that should work very well for Low End Mac, but we're running stuck with templates.
- Will Apple's iPhone/App Store Tornado Blow Away the Competition?, Tim Nash, Taking Back the Market, 10.08. The iPod, iTunes, and the iTunes Store paved the way for the success of the iPhone and the App Store - and nobody can match that.
- More links in our archive.
Go to the Online Tech Journal index.
About LEM | Support | Usage | Privacy | Contacts

