Jump to content


Photo

created an online users script


  • Please log in to reply
1 reply to this topic

#1 tjhilder

tjhilder
  • Members
  • PipPipPip
  • Advanced Member
  • 136 posts
  • LocationKent, UK

Posted 03 May 2006 - 01:21 PM

Hi,

I created my own online users script but it seems to be duplicating entries and I'm not sure why, maybe someone has an idea by looking at my code?

i've added comments at the end of the queries to show what they do (although you'll probably figure that out without the comments.)

$delete_lookup = "DELETE FROM " . $sql_usersonline . " WHERE (date <= '" . $delete_date . "')"; // delete any entries 5 or more minutes old.
if (!$delete_lookup_result = mysql_query($delete_lookup))
{
    echo "<p>Error: nothing deleted because <b>" . mysql_error() . "</b> the query was: " . $delete_lookup;
}
if(isset($signedin))
{
    $user_lookup = "SELECT ip_address FROM " . $sql_usersonline . " WHERE ip_address='" . $_SERVER['REMOTE_ADDR'] . "'"; // check if user exists
    if ($user_lookup_result = mysql_query($user_lookup))
        {
        if(mysql_num_rows($user_lookup_result) == '1')
        {
            $online_db_query = "UPDATE " . $sql_usersonline . " SET date='" . $online_date . "', name='" . $online_name . "', url='" . $online_url . "', member_id='" . $member_id . "', ip_address='" . $online_ip . "' WHERE ip_address='" . $online_ip . "'"; // if user exists, update it.
            $update_members_visit = "UPDATE members SET last_active='" . $date_var . "' WHERE member_id='" . $member_id . "'"; // updates member table to last visited
            if (!$visit_result = mysql_query ($update_members_visit))
            {
                echo "<p>Member's last active date not updated<!-- because: <b>" . mysql_error() . "</b>.</p><p>The query was $update_members_visit -->.</p>";
            }
        }
        else
        {
            $online_db_query = "INSERT INTO " . $sql_usersonline . " (`date`, `name`, `url`, `member_id`, `ip_address`) VALUE ('" . $online_date . "', '" . $online_name . "', '" . $online_url . "', '" . $member_id . "', '" . $online_ip . "')"; // if not exist, insert it.
        }
        if (!$online_result = mysql_query ($online_db_query))
        {
            echo "<p>Online Users not updated<!-- because: <b>" . mysql_error() . "</b>.</p><p>The query was $online_db_query -->.</p>";
        }
    }
    else
    {
        echo "<p>Error: <b>" . mysql_error() . "</b>.</p><p>The query was $user_lookup.</p>";
    }
}
else
{                          
    $guest_lookup = "SELECT ip_address FROM " . $sql_usersonline . " WHERE ip_address='" . $_SERVER['REMOTE_ADDR'] . "'"; // no user, check if guest exists.
    if ($guest_lookup_result = mysql_query($guest_lookup))
        {
        if(mysql_num_rows($guest_lookup_result) == '1')
        {
            $online_db_query = "UPDATE " . $sql_usersonline . " SET date='" . $online_date . "', name='" . $online_name . "', url='" . $online_url . "', member_id='', ip_address='" . $online_ip . "' WHERE ip_address='" . $online_ip . "'"; // if guest exists, update it.
        }
        else
        {
            $online_db_query = "INSERT INTO " . $sql_usersonline . " (`date`, `name`, `url`, `member_id`, `ip_address`) VALUE ('" . $online_date . "', '" . $online_name . "', '" . $online_url . "', '', '" . $online_ip . "')"; // if guest doesn't exist, insert it.
        }
        if (!$online_result = mysql_query ($online_db_query))
        {
            echo "<p>Online Users, not updated<!-- because: <b>" . mysql_error() . "</b>.</p><p>The query was $online_db_query -->.</p>";
        }
    }
    else
    {
        echo "<p><b>" . mysql_error() . "</b>.</p><p>The query was $guest_lookup.</p>";
    }
}


#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 04 May 2006 - 03:44 AM

Ugh.. don't post entire scripts. It sounds like you might be POSTing to this page multiple times. Your INSERT statement isn't in a loop, so that can't be it.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users