Adding Data with Your Browser
Website Automation with PHP and MySQL, Part 8
Low End Mac Reader Specials
Memory To Go Special: MacPro 8 Core Memory 4GB kit $192 / 2GB kit $109. MacBook Pro / MacMini / iMac Intel Core2 DUO 2GB $44 1GB $23--Free shipping available.
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: LA Computer Company: Specials on AppleCare, Apple Displays, MacBooks, iMac's, MacBook Pros, Laptop and iPod accessories and more. Apple A/C Adapters for laptops starting at $25.00 Call 1-800-941-7654 or Click Here.
Other World Computing: Go Longer Between Charges with a NewerTech NuPower Batteries! Not just another battery, NuPower available for PowerBook and iBook models, is built in the USA to offer the longest Run Time + the longest useful LifeTime too! In Stock at OWC from $99.95
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.
NEW MacPro Memory 800Mhz With Apple Spec Heat Sink 2GB Kit $104 / 4GB Kit $184 / 8GB Kit $362 Click to Maximize your Macs...
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.
- More in the Online Tech Journal index.
Recent Content on Low End Mac
- Apple's eMate still a great tool in the classroom, Tommy Thomas, Welcome to Macintosh, 05.09. How one teacher equipped his classroom with eMates with his own money - and plans to keep using them as long as possible.
- $199 iPhone coming?, iPod not a Walkman, crosswalk danger, iPods taking over cars, and more, iNews Review, 05.09. Also the iPhone is a second-rate phone, iPhone 2.0 may introduce handwriting recognition, Kensington battery pack and chargers, new iPhone apps, and more.
- Best Power Mac G4 deals, Low End Mac Deals, 05.09. Used Cube, $479; 400 MHz PCI, $70; 450 AGP, $105; 733 DA, $150; 867 QS, $200; 1 GHz, $250; 450 dual, $295; 1 GHz dual, $400; 1.42, $600; more.
- Best 15" MacBook Pro deals, Low End Mac Deals, 05.09. Used 1.83 GHz Core Duo, $1,100; 2.16, $1,295; refurb, 2.2 Core2, $1,449; 2.4 Penryn, $1,699; 2.5, $2,149; new 2.2, $1,525 after rebate; 2.4, $1,685 a/r; more.
- MacBook sales explode, MacBook Air reviews, several new hard drives, and more, The 'Book Review, 05.09. Also silver-zinc batteries may outlast lithium-ion, Bell Aliant bundling MacBook with Internet access, notebook drives benchmarked, bargain 'Books from $150 to $2,699, and more.
- Best iPod touch deals, Low End Mac Deals, 05.09. Refurb 8 GB '08, $249; 16 GB '07, $329; '08, $349; new 8 GB '07. $269; '08, $280; 16 GB '07, $330; '08, $369; 32 GB, $475.
- More G4 upgrade advice, secure disk wipes, 500 MHz iMacs with Tiger in action, and more, Dan Knight, Low End Mac Mailbag, 05.09. The importance of securely clearing your hard drive before you pass on your Mac, Pismo and closed lid mode, G3 iMacs in the classroom, and more thoughts on upgrading G4 Power Macs.
- Apple tops in tech support, Penryn iMacs and Psystar Open Computer reviewed, and more, Mac News Review, 05.09. Also the iMac philosophy, OpenOffice 3.0 going Mac, MozyHome backup comes to Macs, weather in the Dock, and more.
- 140 million copies of Vista sold (yawn), Frank Fox, Stop the Noiz, 05.09. It sounds like a lot, but over 85% of Windows users are staying away from Vista. 20% of Mac users have embraced Leopard in one-third the time.
- Mac of the Day: Macintosh LC, Oct. 1990 - only 3" tall, the LC was the least expensive color Mac in 1990.
- List of the Day: Jaguar List is for anyone using Mac OS X 10.2.x.
- May 12 in LEM history: 99: Is Apple missing the boat? - 00: PowerBook history - Frankenstein Power Mac - 03: Beige Power Mac G3 - Is a 5400 worth buying? - Upgrades for the tray-loading iMac - Quiet computing - 04: Windows stability: Nothing changes - Broadband Internet access: Picking the right speed - 06: The future of PowerPC Macs in the Intel era - Setting up a 68040-based Mac media center - Mac mini Core Duo upgrades
- Why one Mac user chose BlackBerry over iPhone, Andrew J Fishkin, Best Tools for the Job, 05.08. The advantages of OS X, Safari, Mail, and iSync don't outweigh the familiarity of BlackBerry, its excellent software, easily replaceable batteries, and a camera-free option.
- 500 MHz iMac with Panther great for Internet, watching video, and more, Carl Nygren, My Turn, 05.08. At $65 with upgraded RAM and a bigger hard drive, it was too good to pass up, and it works very nicely with Mac OS X 10.3.
- Boomerang: The Blue and White Power Mac G3 that kept coming back, Charles Webb, The Webb Chronicles, 05.08. Over its nine-year lifespan, this Power Mac had at least five owners before it finally gave up the ghost.
- Best Intel iMac deals, Low End Mac Deals, 05.08. Used 17" 1.83 GHz, $699; 20" 2.16 Core2, $885; refurb 20" 2.16, $949; 2.4, $1,099; 24" 2.16, $1,199; 2.4, $1,399; 2.8, $1,599; Penryn from $1,049 after rebate.
- Best 17" PowerBook G4 deals, Low End Mac Deals, 05.08. Used 17" 1 GHz, $790; 1.33 GHz, $850; 1.5 GHz, $859; 1.67 GHz, $889.
- Best Mac OS X 10.5 'Leopard' deals, Low End Mac Deals, 05.08. Mac OS X 10.5.1 single user, $99; 5 users, $139; 10.5 Server, 10 users, $450; unlimited, $899.
- More links in our archive.
Go to the Online Tech Journal index.
About LEM | Support | Usage | Privacy | Contacts



