At first I'm beginner at php and MySQL. Now I'm working on my first bigger website with Administration panel. I want to do a simple visit counter with mysql and php. I made this code, where visitors IP save in $ip and Insert in DB. Looks easy, but it doesn't work :D I spend about 2 hours on it, but I can't find a mistake in my code. It doesn't show me any information in DB.
include 'connect_db.php';
$ip = $_SERVER['REMOTE_ADDR'];
$sql = mysql_query("SELECT * FROM table WHERE visits='$ip'");
if (!$ip == $sql) {
$insert = mysql_query("INSERT INTO table (visits) VALUES '$ip'");
}
I have 2 items in my table. 1) ID with Auto Increment and 2) visits defined as VARCHAR 50. Server connection is ok, doesn't show any error.
Can you help me please?
答案 0 :(得分:0)
试试这个。 我希望它能提供帮助
include 'connect_db.php';
$ip = $_SERVER['REMOTE_ADDR'];
if (isset($ip)){
$sql = mysql_query("SELECT * FROM table WHERE visits = '$ip'");
if(mysql_num_rows($sql)==0){
$insert = mysql_query("INSERT INTO table (visits) VALUES ('$ip')");
}
}
答案 1 :(得分:-1)
Thank you, I found the mistake .. one of them was statement (Thank you ImAtWar) and other mistake was after "VALUES" in INSERT must be in () ...
But it didn´t work properly. It insert IP in db again even when is inserted once. :l
Repaired code:
$sql = mysql_query("SELECT * FROM visitcont WHERE visits='$ip'");
if ($ip !== $sql) {
$insert = mysql_query("INSERT INTO visitcount (visits) VALUES ('$ip')");
答案 2 :(得分:-1)
你必须使用mysqli或PDO而不是mysql。但是现在试试这个:
include 'connect_db.php';
$ip = $_SERVER['REMOTE_ADDR'];
$sql = mysql_query("SELECT * FROM table WHERE visits= $ip");
if (!empty($ip) && ($ip != $sql)) {
$insert = mysql_query("INSERT INTO table (visits) VALUES $ip");
}else{
echo "already inserted";
}