www.webdeveloper.com
Results 1 to 13 of 13

Thread: Code adding 2 instead of 1

  1. #1
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280

    Code adding 2 instead of 1

    I wanted to make an image counter (Like for craigs list or something) but for some reason, the count always goes up two.
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    if(!isset(
    $_GET['id'])){
        
    $hits "script-x.net";
        
    $font 'fonts/astronau.ttf';
    }else{
        
    $host "@@@@@@@";
        
    $user "@@@@@@@";
        
    $pass "@@@@@@@";
        
    $database "@@@@@@@";
        
    $con mysql_connect($host,$user,$pass);
        if(!
    $con){
            die(
    "FATAL ERROR:".mysql_error());
        }else{
            
    mysql_select_db($database,$con);
        }
        
    $id mysql_real_escape_string(str_replace('.png','',$_GET['id']));
        
    $id str_replace('.jpg','',$id);
        
    $get mysql_query("SELECT * FROM table WHERE id = '".$id."'");
        while(
    $row mysql_fetch_array($get)){
            
    $hits $row['hits'];
            
    $font 'fonts/'.$row['font'];
        }
        
    $hits++;
        
    //$hits = $id;
        
    mysql_query("UPDATE table SET hits = '".$hits."' WHERE id = '".$id."'");
    }
    $img imagecreate(20030);

    $background imagecolorallocate$img,255255255 );

    $text_colour imagecolorallocate$img00);

    imagesetthickness $img);

    imagettftext($img200524$text_colour$font$hits);

    //header( 'Content-type: image/jpeg' );
    header('Content-type: image/png');

    //imagejpeg( $img );
    imagepng($img);


    imagecolordeallocate$text_color );
    imagecolordeallocate$background );
    imagedestroy$img );
    ?>
    It can be access at http://script-x.net/craigslist/1.jpg (with mod rewrite) or at http://script-x.net/craigslist/counter.php?id=1

    It happens either way. Anybody see why?

  2. #2
    Join Date
    Aug 2012
    Posts
    155
    I haven't really looked at why this is happening, but just change your query (much easier) to
    PHP Code:
     $get mysql_query("SELECT font FROM table WHERE id = '".$id."'"); 
        while(
    $row mysql_fetch_array($get)){ 
            
    $font 'fonts/'.$row['font']; 
        } 
        
    mysql_query("UPDATE table SET hits = hits+1 WHERE id = '".$id."'"); 

  3. #3
    Join Date
    Dec 2012
    Posts
    9
    Why still use the mysql set of functions? you really should be using either mysqli or PDO even php.net suggest this as to they have plans on taking the mysql set of functions out completely in the near future or rather making them depreciated

  4. #4
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    I know this. But that doesn't answer my question.

  5. #5
    Join Date
    Dec 2012
    Posts
    9
    Yea im not answering ur question im giving u advice either take that or leave it

  6. #6
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    When I click the link, it opens in my browser (as expected). If I then click refresh over and over, it goes up only 1 each time I click refresh. As far as I can tell that means its functioning properly.

  7. #7
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    Hmm, for me, it goes up by 2 each time.

  8. #8
    Join Date
    Dec 2012
    Posts
    9
    works fine for me to

  9. #9
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    I tried it in chrome and it works....it seems firefox is requesting it twice.

  10. #10
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    Oh firefox, why do you do some strange things that no one ever expects. I just thought of 3 ways to prevent this, and loopholes in each theory. I wonder if its just going to the file directly. Can you try putting the url in the src of an img tag, and see if it still requests twice? If not then there's no reason to worry, otherwise the counter will just be inaccurate.

  11. #11
    Join Date
    Dec 2012
    Posts
    9
    Im using firefox and it works fine

  12. #12
    Join Date
    Nov 2012
    Posts
    2
    yea I've seen,
    thats works, and fine,
    but perfection need more to do...

  13. #13
    Join Date
    Aug 2012
    Posts
    155
    Have you actually implemented the code I posted (updating the count in the query rather than before)??

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles