Weird mysql_query reaction

All the small snippet of code below is supposed to do is take your ip address and query the db for any matches. If a match is found, it then checks to see if the match was created the same day. If so it increases the visits by 1, if not it inserts your ip back into the db for that day’s date.

For some odd reason its not working, nor is it giving me any errors.

[php]// DB Query
$results = mysql_query(“SELECT ip FROM visitors WHERE ip = ‘$IP’”);

// IP was not found
if(empty($results))
{
// Put IP in DB
mysql_query(“INSERT INTO visitors (id, ip, date, visits) VALUES (NULL, ‘$IP’, ‘$DATE’, ‘1’)”);
} else
{
// Was IP visit on today’s date?
$results = mysql_query(“SELECT ip FROM visitors WHERE ip = ‘$IP’ AND date = ‘$DATE’”);

// IP not found for today’s date.
if(empty($results))
{
mysql_query(“INSERT INTO visitors (id, ip, date, visits) VALUES (NULL, ‘$IP’, ‘$DATE’, ‘1’)”);
} else
{
mysql_query(“UPDATE visitors SET visits = ‘200’ WHERE ip = ‘$IP’ AND date = ‘$DATE’”);
}
}[/php]

The connection to the DB are working it should but that still doesn’t explain why nothing is happening. I’ve tried inserting the IP into the DB manually to see if it would update the visits count, and it didn’t. I tried emptying the DB to see if it would insert the IP and it still did nothing.

What’s weird is if I put a “echo” in the very last else statement where it’s supposed to be updating the counts of visits, it displays the echo, so I know it is executing the code all the way to the last if-else statement, but it still isn’t making any updates or changes in the DB like it’s supposed to be doing.

hello subless, please do below things 1. i think your visits field is define as integer in your database table than please remove single quote when inserting or updating values. use below query mysql_query("INSERT INTO `visitors` (`id`, `ip`, `date`, `visits`) VALUES (NULL, '$IP', '$DATE', 1)"); Note: never use quote's around any integer field in query. 2. if you get still error than echo your query first and execute it direct in you database table and check and post what error you getting there.

I hope this will helpful for you.
SR

Sponsor our Newsletter | Privacy Policy | Terms of Service