lukerodham Posted May 13, 2011 Share Posted May 13, 2011 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/236290-mysql-insert-problem/ Share on other sites More sharing options...
gizmola Posted May 13, 2011 Share Posted May 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236290-mysql-insert-problem/#findComment-1214837 Share on other sites More sharing options...
lukerodham Posted May 13, 2011 Author Share Posted May 13, 2011 ok well what i was aiming for was to create a unique ip database, for a stats page and with the WHERE clause would i then just use the mysql_num_row function to check if there is already an entry for the $guestip. if you understand what i mean. Quote Link to comment https://forums.phpfreaks.com/topic/236290-mysql-insert-problem/#findComment-1214839 Share on other sites More sharing options...
gizmola Posted May 13, 2011 Share Posted May 13, 2011 Yes you can do that, OR you can do SELECT count(*) from IP_Address where ip = ... One other thing -- you're needlessly wasting storage. You should use an integer in the database and convert the ip using [m]ip2long[/p]. Quote Link to comment https://forums.phpfreaks.com/topic/236290-mysql-insert-problem/#findComment-1214846 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.