PDO SQL插入语法问题

时间:2017-09-03 16:05:52

标签: php mysql pdo

我试图让我的页面一次向我的数据库插入多行。所有信息均来自上一页。输入某个坐标时,该行不会被保存。

在测试时,只需Alpha获得一个与排除坐标不同的坐标。

当我运行代码时,出现以下错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 2

这是我的代码:

try {
                        $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);

                        $dag = mysqli_real_escape_string($conn, $_GET['dag']);
                        $tijd = mysqli_real_escape_string($conn, $_GET['tijd']);

                        $wgs_coordinaat_x_alpha = mysqli_real_escape_string($conn, $_GET["x_alpha"]);
                        $wgs_coordinaat_y_alpha = mysqli_real_escape_string($conn, $_GET["y_alpha"]);
                        $wgs_coordinaat_x_bravo = mysqli_real_escape_string($conn, $_GET["x_bravo"]);
                        $wgs_coordinaat_y_bravo = mysqli_real_escape_string($conn, $_GET["y_bravo"]);
                        $wgs_coordinaat_x_charlie = mysqli_real_escape_string($conn, $_GET["x_charlie"]);
                        $wgs_coordinaat_y_charlie = mysqli_real_escape_string($conn, $_GET["y_charlie"]);
                        $wgs_coordinaat_x_delta = mysqli_real_escape_string($conn, $_GET["x_delta"]);
                        $wgs_coordinaat_y_delta = mysqli_real_escape_string($conn, $_GET["y_delta"]);
                        $wgs_coordinaat_x_echo = mysqli_real_escape_string($conn, $_GET["x_echo"]);
                        $wgs_coordinaat_y_echo = mysqli_real_escape_string($conn, $_GET["y_echo"]);
                        $wgs_coordinaat_x_foxtrot = mysqli_real_escape_string($conn, $_GET["x_foxtrot"]);
                        $wgs_coordinaat_y_foxtrot = mysqli_real_escape_string($conn, $_GET["y_foxtrot"]);

                        // set the PDO error mode to exception
                        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                        // begin the transaction
                        $conn->beginTransaction();
                        // our SQL statements
                        if ($wgs_coordinaat_x_alpha == "47.9747674834") {}
                            else {
                                $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) 
                                VALUES (NULL, 'Hint', '$dag', '$tijd', 'Alpha', '$wgs_coordinaat_x_alpha', '$wgs_coordinaat_y_alpha')");
                            }
                        if ($wgs_coordinaat_x_bravo == "47.9747674834") {}
                            else {
                                $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) 
                                VALUES (NULL, 'Hint', '$dag', '$tijd', 'Bravo', '$wgs_coordinaat_x_bravo', '$wgs_coordinaat_y_bravo'))");
                            }
                        if ($wgs_coordinaat_x_charlie == "47.9747674834") {}
                            else {
                                $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) 
                                VALUES (NULL, 'Hint', '$dag', '$tijd', 'Charlie', '$wgs_coordinaat_x_charlie', '$wgs_coordinaat_y_charlie')");
                            }
                        if ($wgs_coordinaat_x_delta == "47.9747674834") {}
                            else {
                                $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) 
                                VALUES (NULL, 'Hint', '$dag', '$tijd', 'Delta', '$wgs_coordinaat_x_delta', '$wgs_coordinaat_y_delta')");
                            }
                        if ($wgs_coordinaat_x_echo == "47.9747674834") {}
                            else {
                                $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) 
                                VALUES (NULL, 'Hint', '$dag', '$tijd', 'Echo', '$wgs_coordinaat_x_echo', '$wgs_coordinaat_y_echo')");
                            }
                        if ($wgs_coordinaat_x_foxtrot == "47.9747674834") {}
                            else {
                                $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) 
                                VALUES (NULL, 'Hint', '$dag', '$tijd', 'Foxtrot', '$wgs_coordinaat_x_foxtrot', '$wgs_coordinaat_y_foxtrot')");
                            }   
                        // commit the transaction
                        $conn->commit();
                        echo "Hint succesvol toegevoegd!";
                        }
                    catch(PDOException $e)
                        {
                        // roll back the transaction if something failed
                        $conn->rollback();
                        echo "Opes! Iets is fout gegaan: " . $e->getMessage();
                        }

                    $conn = null;

我没有想法。谢谢!

3 个答案:

答案 0 :(得分:1)

此行中的双括号:

VALUES (NULL, 'Hint', '$dag', '$tijd', 'Bravo', '$wgs_coordinaat_x_bravo', '$wgs_coordinaat_y_bravo'))");

答案 1 :(得分:0)

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);

    $dag = mysqli_real_escape_string($conn, $_GET['dag']);
    $tijd = mysqli_real_escape_string($conn, $_GET['tijd']);

    $wgs_coordinaat_x_alpha = mysqli_real_escape_string($conn, $_GET["x_alpha"]);
    $wgs_coordinaat_y_alpha = mysqli_real_escape_string($conn, $_GET["y_alpha"]);
    $wgs_coordinaat_x_bravo = mysqli_real_escape_string($conn, $_GET["x_bravo"]);
    $wgs_coordinaat_y_bravo = mysqli_real_escape_string($conn, $_GET["y_bravo"]);
    $wgs_coordinaat_x_charlie = mysqli_real_escape_string($conn, $_GET["x_charlie"]);
    $wgs_coordinaat_y_charlie = mysqli_real_escape_string($conn, $_GET["y_charlie"]);
    $wgs_coordinaat_x_delta = mysqli_real_escape_string($conn, $_GET["x_delta"]);
    $wgs_coordinaat_y_delta = mysqli_real_escape_string($conn, $_GET["y_delta"]);
    $wgs_coordinaat_x_echo = mysqli_real_escape_string($conn, $_GET["x_echo"]);
    $wgs_coordinaat_y_echo = mysqli_real_escape_string($conn, $_GET["y_echo"]);
    $wgs_coordinaat_x_foxtrot = mysqli_real_escape_string($conn, $_GET["x_foxtrot"]);
    $wgs_coordinaat_y_foxtrot = mysqli_real_escape_string($conn, $_GET["y_foxtrot"]);

    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // begin the transaction
    $conn->beginTransaction();

    // our SQL statements
    if ($wgs_coordinaat_x_alpha != "47.9747674834") {
        $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) VALUES (NULL, 'Hint', '$dag', '$tijd', 'Alpha', '$wgs_coordinaat_x_alpha', '$wgs_coordinaat_y_alpha')");
    }
    if ($wgs_coordinaat_x_bravo != "47.9747674834") {
        $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) VALUES (NULL, 'Hint', '$dag', '$tijd', 'Bravo', '$wgs_coordinaat_x_bravo', '$wgs_coordinaat_y_bravo')");
    }
    if ($wgs_coordinaat_x_charlie != "47.9747674834") {
        $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) VALUES (NULL, 'Hint', '$dag', '$tijd', 'Charlie', '$wgs_coordinaat_x_charlie', '$wgs_coordinaat_y_charlie')");
    }
    if ($wgs_coordinaat_x_delta != "47.9747674834") {
        $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) VALUES (NULL, 'Hint', '$dag', '$tijd', 'Delta', '$wgs_coordinaat_x_delta', '$wgs_coordinaat_y_delta')");
    }
    if ($wgs_coordinaat_x_echo != "47.9747674834") {
        $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) VALUES (NULL, 'Hint', '$dag', '$tijd', 'Echo', '$wgs_coordinaat_x_echo', '$wgs_coordinaat_y_echo')");
    }
    if ($wgs_coordinaat_x_foxtrot != "47.9747674834") {
        $conn->exec("INSERT INTO markers (id, type, dag, tijd, deelgebied, lat, lng) VALUES (NULL, 'Hint', '$dag', '$tijd', 'Foxtrot', '$wgs_coordinaat_x_foxtrot', '$wgs_coordinaat_y_foxtrot')");
    }

    // commit the transaction
    $conn->commit();
    echo "Hint succesvol toegevoegd!";
}
catch(PDOException $e)
{
    // roll back the transaction if something failed
    $conn->rollback();
    echo "Opes! Iets is fout gegaan: " . $e->getMessage();
}

$conn = null;

在线关闭括号:

VALUES (NULL, 'Hint', '$dag', '$tijd', 'Bravo', '$wgs_coordinaat_x_bravo', '$wgs_coordinaat_y_bravo'))");

我将你的if语句切换为使用!=,然后我们删除了额外的else语句。此外,由于您的coords(47.9747674834)在所有语句中都相同,请考虑将其缓存在变量中。

答案 2 :(得分:0)

正如其他人所说,在插入Bravo坐标的行上,SQL中有两个右括号。

但我必须评论一些其他最佳做法,与您的语法错误无关。

您应该使用查询参数而不是将变量插入到SQL字符串中。这样更容易,更安全,因为它可以帮助您避免语法错误。

此外,您正在将mysqli函数与PDO混合使用。

以下是我编写此代码的方法:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);

    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

   if (isset($_GET['dag']) {
       $dag = $_GET['dag'];
   } else {
       die("dag is verplicht");
   }
   if (isset($_GET['tijd']) {
       $tijd = $_GET['tijd'];
   } else {
       die("tijd is verplicht");
   }

    // begin the transaction
    $conn->beginTransaction();

    // our SQL statements
    $stmt = $conn->prepare("INSERT INTO markers
        SET type='Hint', dag=:dag, tijd=:tijd, deelgebied='Alpha', lat=:lat, lng=:lng");

    foreach (['alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot'] as $coord) {

        if (isset($_GET["x_$coord"]) && $_GET["x_$coord"] != "47.9747674834") {
            $stmt->execute([
                'dag'=>$dag,
                'tijd'=>$tijd,
                'lat'=>$_GET["x_$coord"],
                'lng'=>$_GET["y_$coord"]
            ]);
        }
    }

    // commit the transaction
    $conn->commit();
    echo "Hint succesvol toegevoegd!";
}
catch(PDOException $e)
{
    // roll back the transaction if something failed
    $conn->rollback();
    echo "Opes! Iets is fout gegaan: " . $e->getMessage();
}

$conn = null;