Jump to content

Adding ip to data bases


bob2588

Recommended Posts

hello i am writing  a script that will add every visitor form my site ip to an data base but there is a problem one it should check to see if the ip has been added today and the second it add the ip twice to the data bases when it should only be add it once here is the code

 

<?php
$ip = $_SERVER['REMOTE_ADDR'];  
$date = date('M,d,Y');
$host = "localhost"; //database location
$user = "user"; //database username
$pass = "pass"; //database password
$db_name = "ip"; //database name
//database connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);
$check = mysql_query("SELECT * FROM ip WHERE ip='$ip'");
while($row = mysql_fetch_array($check));
if ($row['ip']=="$ip" & $row['date']=="$date") {
mysql_close($link);
}
else
{
mysql_query("INSERT INTO ip (id, ip, date)
VALUES ( NULL, '$ip', '$date' )")
or die(mysql_error());
mysql_close($link);
}
?>

 

Thank for the help

 

bob

Link to comment
Share on other sites

you've got while infront of the mysql_fetch_array but theres no commands being executed within that while loop... its being ended by the semi-colon straight after it...

 

use this if theres only one row per IP...

<?php
$ip = $_SERVER['REMOTE_ADDR'];  
$date = date('M,d,Y');
$host = "localhost"; //database location
$user = "user"; //database username
$pass = "pass"; //database password
$db_name = "ip"; //database name
//database connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);
$check = mysql_query("SELECT * FROM ip WHERE ip='$ip'");
$row = mysql_fetch_array($check);
if ($row['ip']=="$ip" & $row['date']=="$date") {
mysql_close($link);
}
else
{
mysql_query("INSERT INTO ip (id, ip, date)
VALUES ( NULL, '$ip', '$date' )")
or die(mysql_error());
mysql_close($link);
}
?>

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.