php代码给出了意想不到的t_variable

时间:2014-12-15 16:48:58

标签: php

检查以下代码时出现此错误:

解析错误:语法错误,第44行代码中意外的'$ entry'(T_VARIABLE)

else{
        foreach($results as $result) {
            foreach($result as $entry) {
                echo $entry;
                $sql = "INSERT INTO city (ID, Name, CountryCode, District, Population)
                        VALUES ('"$entry + 1, "'"$_POST["name"]"'", "'"$_POST["countryCode"]"'", "'"$_POST["district"]"'", "'"$_POST["population"]"');";
            }
        }

        mysqli_query($conn, $sql);
    }

我希望有人可以指出(可能是显而易见的)问题。

2 个答案:

答案 0 :(得分:2)

您需要按如下方式连接SQL语句:

$sql = "INSERT INTO city (ID, Name, CountryCode, District, Population)
        VALUES ('".($entry + 1).", '".$_POST["name"]."', '".$_POST["countryCode"]."', '".$_POST["district"]."', '".$_POST["population"]."')";

答案 1 :(得分:0)

试试这个:

$sql = "INSERT INTO city (ID, Name, CountryCode, District, Population)
        VALUES ('" . ($entry + 1) . "', '" . $_POST["name"] . "', '" . $_POST["countryCode"]. "', '". $_POST["district"]. "', '" . $_POST["population"] . "');";