Jump to content

[SOLVED] Code Help


billynastie

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.