Simple Stat Tracker

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

saltriver
Forum Commoner
Posts: 59
Joined: Fri Mar 12, 2004 2:40 pm
Location: Burlington, VT

SOLVED

Post by saltriver »

if (!$num_rows) {

That's the little bugger.

So here it is in it's final form:

Code: Select all

$recordID = $_GET['recordID'];
$db = mysql_select_db($db_name, $db_handle) or die (mysql_error());
$rc = "select * from popup where rest = '$recordID'";
$restchk = mysql_query($rc) or die (mysql_error());
$num_rows = mysql_num_rows($restchk);
if (!$num_rows) {
$sql = "insert into popup values ('', '$recordID', now(), '1')"; 
mysql_query($sql);
} else {
$sqlu = "update popup set hits = hits ++1 where rest = '$recordID'";
mysql_query($sqlu);
};
Muchos Gacias, ole!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You are:

- not validating user input ($_GET['recordID'] (http://www.php.net/isset)
- you don't prepare the recordID for use in a mysql query (http://www.php.net/mysql_real_escape_string)

Btw, since you are using mysql you can use the following:

Code: Select all

INSERT INTO foo VALUES (bar)
ON DUPLICATE KEY UPDATE countcol = countcol + 1
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

my code relies on the id being an integer, and it should be for performance reasons. using a string as the id is sloooow
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I've never said anything about using (VAR)CHAR / STRING as datatype... Btw, with the query i suggested you only have one query.. Which is more performant than two ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

So many cool little tricks and tips I've learned from you timv... keep em coming. :wink:
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

INSERT INTO foo VALUES (bar)
ON DUPLICATE KEY UPDATE countcol = countcol + 1
oh wow that's very cool
Post Reply