billynastie Posted June 28, 2009 Share Posted June 28, 2009 I am writing a little chunk of code on a site for recording basic tracking I have managed to write most of the code but im having problems basically its inserting into the database ip address etc then when someone comes back it is supposed to check the database field first to see if the ip is already there if it is it updates the details if its not it enters a new detail I have tried several ways to do this none of which have been successful and im now at the end of the road with variations i am not a php programmer and need help getting my head round this I do not want to use sessions as I am incorporating this into a website baker page and it already creates sessions so i am using the database instead. <? mysql_connect("localhost", "eyedesir_test", "test") or die(mysql_error()); mysql_select_db("eyedesir_test") or die(mysql_error()); // Getting the information $start = "SELECT * FROM log"; $res = mysql_query($start) or die(mysql_error()); while($row = mysql_fetch_array( $res )) { $ipaddress = $_SERVER['REMOTE_ADDR']; $page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}"; $datetime = mktime(date("Y"),date("d"),date("m"),0,0,0); if ($ipaddress == "$ippulled") { mysql_query("UPDATE log SET `datetime` = NOW(), `page` = '$page' WHERE `ipaddress` = '$ipaddress' ") or die(mysql_error()); } } $insert = "SELECT * FROM log"; $result = mysql_query($insert) or die(mysql_error()); while($range = mysql_fetch_array( $result )) { $ippull = $range['ipaddress']; if ($ipaddress !== "$ippull") { $enter = "INSERT INTO `log` (`ipaddress`, `datetime`, `page`) VALUES('$ipaddress', NOW(), '$page') "; mysql_query($enter) or die(mysql_error()); } } ?> as you can see im using two if and while loops this is because i have tried to do an if else inside a while loop and it didnt work and i cannot get this to work all i want the code to do it put new ip addresses into the database but update the ones which are already there with datetime and page. Quote Link to comment Share on other sites More sharing options...
Alex Posted June 28, 2009 Share Posted June 28, 2009 To make things easier you should make a simple function, to check if it's already in the database, if not add the person, otherwise just update. Something like this: function ip_exists($ip) { $result = mysql_query("SELECT `datetime` FROM `log` WHERE ipaddress='$ip'"); return (!mysql_num_rows($result)) ? false : true; } if(ip_exists($_SERVER['REMOTE_ADDR'])) { //Update } else { //Insert new record } Quote Link to comment Share on other sites More sharing options...
billynastie Posted June 28, 2009 Author Share Posted June 28, 2009 Hi followed what you said Alex and I can get it to work from a independant page but now from within my website baker page it keeps inserting and updating here is the code which I have used. <? mysql_connect("localhost", "eyedesir_test", "test") or die(mysql_error()); mysql_select_db("eyedesir_test") or die(mysql_error()); $ipaddress = $_SERVER['REMOTE_ADDR']; $page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}"; $datetime = mktime(date("Y"),date("d"),date("m"),0,0,0); function ip_exists($ipaddress) { $result = mysql_query("SELECT `datetime` FROM `log` WHERE ipaddress='$ipaddress'"); return (!mysql_num_rows($result)) ? false : true; } if(ip_exists($_SERVER['REMOTE_ADDR'])) { mysql_query("UPDATE log SET `datetime` = NOW(), `page` = '$page' WHERE `ipaddress` = '$ipaddress' ") or die(mysql_error()); } else { $enter = "INSERT INTO `log` (`ipaddress`, `datetime`, `page`) VALUES('$ipaddress', NOW(), '$page') "; mysql_query($enter) or die(mysql_error()); } ?> Am I doing something wrong.... Quote Link to comment Share on other sites More sharing options...
Alex Posted June 28, 2009 Share Posted June 28, 2009 It looks fine at first glance. What do you mean it's inserting and updating? Quote Link to comment Share on other sites More sharing options...
billynastie Posted June 28, 2009 Author Share Posted June 28, 2009 It updates the correct IP address and then inserts a row which has a blank ip all 00000 for the datetime but gets the page correct but it doesnt do this on a live enviroment http://test.eyedesireonline.co.uk see attached image. Try it http://adietech.dyndns.org/barrhilljfc.co.uk/ as you can see its on my home test rig and not on a live site whether that makes a difference i dont know. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
billynastie Posted June 29, 2009 Author Share Posted June 29, 2009 Cheers AlexWD that code you sent did work it was me I had included the code in a seperate file then added it to the main page as well sorry it works fine now thanks again for your help. Quote Link to comment 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.