尝试将值插入数据库时​​出错

时间:2018-08-29 20:13:00

标签: php mysql mysqli

我正在练习PHP,现在,我正在研究使用MySQLi向数据库中插入值,但出现此错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1,但我不知道我在做什么错!这是我的代码:

header('Content-Type: text/html; charset=utf-8');
ini_set("display_errors", 1);

function getUserIP()
{
    // Get real visitor IP behind CloudFlare network
    if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
              $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
              $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
    }
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}

if(isset($_GET['lat']) && !empty($_GET['lat']))
{
    $lat = $_GET['lat'];
} else {
    die();
}

if(isset($_GET['lon']) && !empty($_GET['lon']))
{
    $lon = $_GET['lon'];
} else {
    die();
}

$ip = getUserIP();
require('database.php');

$query = mysqli_query($mysqli, "SELECT * FROM `tracking` WHERE ip='".$ip."'");
if (!$query)
{
    die('Error: ' . mysqli_error($mysqli));
}
if(mysqli_num_rows($query) > 0){
    mysqli_query($mysqli,"UPDATE `tracking` SET lat='".$lat."', lon='".$lon."' WHERE ip='".$ip."'") or die(mysqli_error($mysqli)); echo "x1";
}
if(mysqli_num_rows($query) == 0){
    mysqli_query($mysqli,"INSERT INTO `tracking` (id, ip, lat, lon) VALUES (NULL, '".$ip."', '".$lat."', '".$lon."'") or die(mysqli_error($mysqli));
}
mysqli_close($mysqli);

给出错误的行是:

mysqli_query($mysqli,"INSERT INTO跟踪(id, ip, lat, lon) VALUES (NULL, '".$ip."', '".$lat."', '".$lon."'") or die(mysqli_error($mysqli));

希望您能理解我在这里所说的话。你能帮助我吗?谢谢!

0 个答案:

没有答案