简体   繁体   中英

If statement or while loop failing on me in php

I am fairly new to coding in php and have come accross a bit of a problem which I think is very simple. I am trying to capture the IP address of anybody that visits my website and stores that in my database. The code is as follows, I am not even sure the code will work but I do get the following error Parse error: syntax error, unexpected T_INC on line 29 which I will highlight in the code:

$ip = $_SERVER['HTTP_CLIENT_IP']; 
                $query="SELECT * FROM ip";
                $result = mysql_query($query);
                $num = mysql_numrows($result);
                $i = 1;
                $found=false;
                while(($i - 1) < $num){
                    $selection = mysql_query("SELECT ip FROM ip WHERE id=$i");
                    $tip = mysql_fetch_assoc($selection);
                    if($tip == $ip){
                        $found = true;
                    }
                i++; //This is line 29
                }
                if($found == false){
                    $sql = "INSERT INTO `rowley_blog`.`ip` (`ip`) VALUES ('$ip');";
                    mysql_query($sql);
                    mysql_close();
                }

您需要使用前缀变量$ ,所以它的$i++

它应该是这样的:

$i++;

你需要在所有变量之前加上一个美元符号 - 应该是这样

$i++;

Apart from the syntax error: This code seems blown-up to me. Doesn't this do the same:

$ip = $_SERVER['HTTP_CLIENT_IP'];
mysql_query("REPLACE INTO `ip` (`ip`) VALUES ('" . mysql_real_escape_string($ip) . "')");

Remember: Good code is short code ;)

Furthermore: If you are only beginning to code PHP, please don't even start using the mysql_ extension. Instead use PDO :

$ip = $_SERVER['HTTP_CLIENT_IP'];
$db->query("REPLACE INTO `ip` (`ip`) VALUES ('" .$db->quote($ip) . "')");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM