Jump to content

Recommended Posts

Hey guys for some reason this code is not working i can't see a problem myself could someone please have a look and point the issue out to me. what i mean by it not working is it won't insert into the database or show a mysql_error.

 

thanks in advance :)

 

$guestip = $_SERVER['REMOTE_ADDR'];
$time = date('G:i');
$date = date("y-m-d");

$query = mysql_query("SELECT * FROM IP_Address") or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
    
    if($guestip != $row['ip']){
        //insert into db.
        mysql_query("INSERT INTO IP_Address(id, ip, date, time) VALUES(NULL,'$questip','$date','$time')") or die(mysql_error());
        echo "inserted in to database"; echo mysql_error();
    }else{
        // add hit count and update time and date.
        echo "already in db";
    }
}

Link to comment
https://forums.phpfreaks.com/topic/236290-mysql-insert-problem/
Share on other sites

There's a substantial fault in the logic of what you are doing here.

 

Does your IP_Address table have anything in it?

 

Consider what happens when IP_Address is empty --  will you ever do an insert?

 

This is not the way to solve this problem -- looking at every row in the table, to determine whether or not there is a row with the same IP address in there is a really bad idea. I'm not sure what the purpose of this table is, but you can determine in advance whether or not there is already a row in the table by using a WHERE clause.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.