Results 1 to 2 of 2

Thread: Add one to integer not working

  1. #1
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Question Add one to integer not working

    Edit: 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 Code:
    <?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):


    Thanks,
    X96
    Last edited by X96 Web Design; 01-01-2010 at 03:19 AM. Reason: resolved
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  2. #2
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    I did it!! Here's the code that works:
    PHP Code:
    <?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 \\
    Last edited by X96 Web Design; 01-01-2010 at 03:17 AM. Reason: removed db connection info
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •