Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5996993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:11:59+00:00 2026-05-23T00:11:59+00:00

i have this code : <?php echo $_POST[‘id-input’]; $time = time(); $ip=$_SERVER[‘REMOTE_ADDR’]; $userid_c =

  • 0

i have this code :

   <?php
    echo $_POST['id-input'];
    $time = time();
    $ip=$_SERVER['REMOTE_ADDR'];
    $userid_c = $_POST['id-input'];
    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

     mysql_select_db("kritya20_data", $con);
     $sql = "SELECT * FROM users_online WHERE user_id='$userid_c'";
     $query = mysql_query($sql);
     $rows_only_c = mysql_num_rows($query);
     if($rows_only_c == 0)
     {
        $sql1= "INSERT INTO users_online (user_id , online_since , lastup , lobbieid)
        VALUES ('$userid_c' , '$time' , '$time' , '1')";
        $query1 = mysql_query($sql1);
     }
     elseif($rows_only_c==1)
     {
        $sql2 = "UPDATE users_online
        SET lastup='$time'
        WHERE user_id='$userid_c' ";
        $query2 = mysql_query($sql2);
     }
     $sql3 = "SELECT * FROM iptable WHERE user_id='$userid_c'";
     $query3 = mysql_query($sql3);
     $row_ip = mysql_num_rows($query3);
     if($row_ip==0)
     {
          $sql4 = "INSERT INTO iptable (user_id , ip , added)
          VALUES ('$userid_c' , '$ip' , '$time')";
          $query4= mysql_query($sql4);
    }
    elseif($row_ip>0)
    {
        $mt=0;
        $mf=0;
        $sql4 = "SELECT * FROM iptable WHERE user_id='$userid_c'";
        $query4 = mysql_query($sql4);
        while($row=$query4)
        {
          if($ip==$row[2])
          {
              $mt++;
          }
          else
          {
              $mf++;
          }
        }
        if($mf!=0)
        {
            $sql5 = "INSERT INTO iptable (user_id , ip , added)
            VALUES ('$userid_c' , '$ip' , '$time')";
            $query5= mysql_query($sql5);
        }
    }
?>

where have been i going wrong ? :O
because either it only adds for first time and then whenever the ip changes it doesnt.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:12:00+00:00Added an answer on May 23, 2026 at 12:12 am

    I have modified the code below for improved layout, descriptive variable names, added debugging, suggested logic improvements which simplify the code and fixed your bug.

    Please read and try to use some of this in your future code, it will help you immensely with debugging or just writing code people can understand. I not trying to sound like an condescending ass, we all started somewhere, and I still make silly mistakes!

    If there is something I have done you do not understand (either the code, or why I did it that way) please ask.

    <?php
        $debug_on = true;
    
        #Variable section
        $time = time();
        $ip=$_SERVER['REMOTE_ADDR'];
        $userid_c = $_POST['id-input'];
    
        if($debug_on) {
            echo "<p>time: '$time'</p>";
            echo "<p>ip: '$ip'</p>";
            echo "<p>userid_c: '$userid_c'</p>";
        }
    
        #Connect to database
        $con = mysql_connect("localhost","username","password");
        if (!$con) {
            die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("kritya20_data", $con) or die('Could not select database');
    
        #Insert or update users_online
        $user_exits_sql = "SELECT * FROM users_online WHERE user_id='$userid_c'";
        $user_exits_query = mysql_query($user_exits_sql);
        $user_exists = mysql_num_rows($user_exits_query);
        if($user_exists == 0) {
            $insert_user_sql = "INSERT INTO users_online (user_id , online_since , lastup , lobbieid) VALUES ('$userid_c' , '$time' , '$time' , '1')";
            if($debug_on) {
                echo "<p>No record found in `users_online` - Inserting new record</p>";
                echo "<p>$insert_user_sql</p>";
            }
            mysql_query($insert_user_sql);
        }
        else { # no need for 'elseif($rows_only_c==1)' as this is the only other option
            $update_user_sql = "UPDATE users_online
                                SET lastup='$time'
                                WHERE user_id='$userid_c' ";
            if($debug_on) {
                echo "<p>Record found in `users_online` - Updating existing record</p>";
                echo "<p>$update_user_sql</p>";
            }
            mysql_query($update_user_sql);
        }
    
        #Insert or update iptable
        $user_ip_exists_sql = "SELECT * FROM iptable WHERE user_id='$userid_c'";
        $user_ip_exists_query = mysql_query($user_ip_exists_sql);
        $user_ip_exists = mysql_num_rows($user_ip_exists_query);
        if($user_ip_exists==0) {
            $user_ip_insert_sql = "INSERT INTO iptable (user_id , ip , added)
                                   VALUES ('$userid_c' , '$ip' , '$time')";
            if($debug_on) {
                echo "<p>No record found in `iptable` - Inserting new record</p>";
                echo "<p>$user_ip_insert_sql</p>";
            }
            mysql_query($user_ip_insert_sql);
        }
        else { # 'elseif($row_ip>0)'
            $mt = 0;
            $mf = 0;
            $iptable_sql = "SELECT * FROM iptable WHERE user_id='$userid_c'";
            $iptable_query = mysql_query($iptable_sql);
            #The next line is your bug,
            #should read while($row = $iptable_query->mysql_fetch_array())
            while($row = $iptable_query) {
              if($ip==$row[2]) {
                  $mt++;
              }
              else {
                  $mf++;
              }
            }
            # For bonus points you could use $iptable_query->mysql_fetch_row(), then $row->ip rather than $row[2]. 
            # This makes it obvious what variable you are trying to access.
            if($mf != 0) {
                $iptable_insert_sql = "INSERT INTO iptable (user_id , ip , added)
                                       VALUES ('$userid_c' , '$ip' , '$time')";
                if($debug_on) {
                    echo "<p>No record found in `iptable` for user with this ip address - Inserting new record</p>";
                    echo "<p>$iptable_insert_sql</p>";
                }
                mysql_query($iptable_insert_sql);
            }
    
            # This whole section seems to be:
            # If user doesnt exist, add them,
            # If user does exist, but not for this ip, add them
            # If this is right this could be made much simpler by changing $user_ip_exists_sql to be where user_id='$userid_c' and ip='$ip'
            # then you could remove all the $mt, $mf stuff
        }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this PHP code echo '<a href=# onclick=updateByQuery(\'Layer3\', ' . json_encode($query) . ');>Link
I have this php code, with which I am trying to generate a popup
Say i have this PHP code: $FooBar = a string; i then need a
I have this code <?php session_start(); if (isset($_GET[cmd])) $cmd = $_GET[cmd]; else die(You should
i need help with disk_total_space function.. i have this on my code <?php $sql=select
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
I have this code, $(function () { $.post(fetch_data.php?url=+$('#url').val(), { }, function(response){ var arrValues =
I have this piece of code I've done so far: $assign = $_POST['assign']; if(!empty($name)
Okay. I have this code on my site: <?php session_start(); include database.php; include bruger.php;
Happy New Year Everyone! I have this existing php code I did when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.