Current location: Hot Scripts Forums » Programming Languages » PHP » Getting updated twice?


Getting updated twice?

Reply
  #1 (permalink)  
Old 06-05-06, 02:35 PM
landboy landboy is offline
Newbie Coder
 
Join Date: May 2004
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Getting updated twice?

Hello,

I am using Linux and I have to script that takes the result and addes 1 too it. But it keeps on adding two too it.. Yet when I print the variables they are only getting added by one.. And the same variables show as being updated by twice when in a mysql_query

Code..
PHP Code:

    php:$query mysql_query("SELECT total_hits, total_uniques FROM stats LIMIT 1") or die("Error1".mysql_error());

    
$data mysql_fetch_array($query);
    
$total_hits $data['total_hits'];
    
$total_uniques $data['total_uniques'];

    
// Get info
    
$ip $_SERVER['REMOTE_ADDR'];

    
$vist mysql_query("SELECT ip FROM log WHERE ip='$ip' LIMIT 1") or die("Error2".mysql_error());
    
$visits mysql_fetch_array($vist);
    if(
mysql_num_rows($vist) == 1) {
      
$new_u $total_uniques;
    }else{
      
$new_u $total_uniques+1;
    }

    
$new_h $total_hits+1;

    
// Update
    
mysql_query("UPDATE stats SET total_hits='$new_h', total_uniques='$new_u'") or die("Error3".mysql_error());
    
?> 


But when I run it.. It addes 2 to the mysql database.. Insted of one.. Is the script somehow getting run twice??

- Steve
__________________
PHP Coder
Reply With Quote
  #2 (permalink)  
Old 06-08-06, 12:36 AM
ngcomputing's Avatar
ngcomputing ngcomputing is offline
Newbie Coder
 
Join Date: Jun 2006
Location: Florida
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Dunno if this helps. I shortened up the code a little bit as you can see. In addition, if you set a session variable to detect if the counter has already been ran, a user can't click on the refresh button all day and update your total_hits over and over. Typically, I would include the counter code in each page of the site via an include file.

Also check your mysql fields and make sure they are the right data types, if your using something like tinyint your going to run into trouble!


PHP Code:

<?php 


@session_start();

function 
update_counters(){
 
    
$query mysql_query("SELECT total_hits, total_uniques FROM stats LIMIT 1") or die("Error1".mysql_error());
    
$data mysql_fetch_array($query);
    
$total_hits $data['total_hits'];
    
$total_uniques $data['total_uniques'];

    
// Get info
    
$ip $_SERVER['REMOTE_ADDR'];

    
$vist mysql_query("SELECT ip FROM log WHERE ip='$ip' LIMIT 1") or die("Error2".mysql_error());

    if(
mysql_num_rows($vist) == 0) {
            
$total_uniques++;
    }

    
$total_hits++;

    
// Update
    
mysql_query("UPDATE stats SET total_hits='$total_hits', total_uniques='$total_uniques'") or die("Error3".mysql_error());

return 
$total_uniques;

// end updatecounters() function


if(!isset($UNIQ_VISITORS)){

 
session_register['UNIQ_VISITORS'];
 
$UNIQ_VISITORS update_counters();


}


// Now you can display $UNIQ_VISITORS on your pages anywhere to show the number of unique visitors to the site since its value was set by the function update_counters()

echo "Unique Site Visitors : $UNIQ_VISITORS";


?>

Last edited by Christian; 06-08-06 at 02:08 PM. Reason: Please read all of the stickys before posting, http://www.programmingtalk.com/showthread.php?t=15024
Reply With Quote
  #3 (permalink)  
Old 06-08-06, 04:38 AM
Grabber's Avatar
Grabber Grabber is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
mysql_query("UPDATE stats SET total_hits='$new_h', total_uniques='$new_u'")
why are you using single quote for integer values in the query?

secondly your error explanation is confusing you said it addes two to the mysql database. well, of course it will add two if the user is ip is unique, one for $total_uniques and one for $total_hits ..

So explain please which one of these two variables is incremented twice?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Updated News blessedrub PHP 1 03-14-06 02:21 PM
Not all fields were updated shakkalakka HotScripts Site Bug Reports 0 10-08-04 01:57 AM
please sort scripts by updated date teletubby Hot Scripts Forum Questions, Suggestions and Feedback 11 08-17-03 12:23 PM
updated listings gregphoto Hot Scripts Forum Questions, Suggestions and Feedback 2 06-25-03 01:54 PM


All times are GMT -5. The time now is 07:16 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.