Jump to content

mysql insert problem :/


lukerodham

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.