Log in

View Full Version : Resolved Add one to integer not working



X96 Web Design
12-31-2009, 06:53 PM
RESOLVED: See next post
I can't get a download-counter script I made to work.

I can get it to redirect to the file, but it's not updating the row... I'm probably overlooking something very simple...

<?php
if(isset($_GET['f'])){
mysql_connect("localhost", "USER", "PASS");
mysql_select_db("DB");
$getf = $_GET['f'];
$sql = mysql_query("SELECT * FROM `downloads` WHERE `name` = '$getf'");
$numrows = mysql_num_rows($sql);
if($numrows == 1){
$sql = mysql_query("SELECT * FROM `downloads` WHERE `name` = '$getf'");
while($rs = mysql_fetch_array($sql)) {
$num = $rs[0] + 1;
}
mysql_query("UPDATE downloads WHERE `name`='$getf' SET `hits` = '$num'");
header("location: wp-content/uploads/".$getf);
}
else{
mysql_query("INSERT INTO `downloads` VALUES('$getf', '1')");
header("location: wp-content/uploads/".$getf);
}
}
else {
echo 'No file specified!';
}
?>

Here's the table structure (from PHPMyAdmin):
http://oqilug.bay.livefilestore.com/y1pVxIf_Zy9Jt5s_dp5kRalTZk3FUEAgmBeSuz7QRlj3CCvJ98MvRrYwZsTTG2Hb4OZFzHsH80I6Z0oeCwtfU8ZT8Z0xvgNMMBp/phpmyadmin_hits_not_updating.jpg

Thanks,
X96

X96 Web Design
01-01-2010, 03:17 AM
I did it!! Here's the code that works:

<?php
if(isset($_GET['f'])){
mysql_connect("localhost", "USER", "PASS");
mysql_select_db("DB");
$getf = $_GET['f'];
$sql = mysql_query("SELECT * FROM `downloads` WHERE `name` = '$getf'");
$numrows = mysql_num_rows($sql);
if($numrows == 0){
mysql_query("INSERT INTO `downloads` VALUES('$getf', '1')");
header("location: wp-content/uploads/".$getf);
}
else{
while($rs = mysql_fetch_array($sql)) {
$num = $rs[1] + 1;
}
mysql_query("UPDATE `downloads` SET `hits` = '$num' WHERE `name` = '$getf'");
header("location: wp-content/uploads/".$getf);
}
}
else {
echo '<div style="background:#fffacd; border:1px solid #fff000; padding:10px; text-align:center; display:block; margin:20px auto 0; width:600px; font-family:Lucida Grande, Lucida Sans, Arial, sans-serif;"><h1>Incorrect Syntax / Missing Header</h1><p><strong>No file was specified.</strong> This could mean the syntax is wrong, or there was no header information sent.</p><p><a href="http://x96-templates.uuuq.com/" style="color:#cc0000;">Back to Homepage</a></p></div>';
}
?>

I had the MySQL query backwards: I had WHERE .. SET when I needed to use SET .. WHERE ....

// X96 \\