PDO error: General error: 2031 [duplicate]

穿精又带淫゛_ 提交于 2019-12-23 17:03:53

问题


I get this error when i'm executing my code. I know this has been discussed a several times here but i couldn't solve my problem by reading the solutions provided there.

This is the error i get: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2031

This is my code:

function insertMarker(){

    $lat = $_POST['lat'];
    $long = $_POST['long'];
    $street = $_POST['street'];
    $city = $_POST['city'];
    $zip = $_POST['zip'];

    echo ("$lat, $long, $street, $city, $zip");
    global $dbconnect;
    $query = $dbconnect->query("INSERT INTO address (latitude, longitude, street, city, zip) VALUES (?,?,?,?,?)");
        $query->bindParam(1, $lat);
        $query->bindParam(2, $long);
        $query->bindParam(3, $street);
        $query->bindParam(4, $city);
        $query->bindParam(5, $zip);
        $query->execute();

        //$query->execute(array(":lat"=>$lat, ":long"=>$long,":street"=>$street,":city"=>$city,":zip"=>$zip));
}

回答1:


Just making my comment an answer:

If dbconnect is an instance of PDO then query both creates a prepared statement and then executes it all in one go. So its not getting the parameters bound initially. Use PDO::prepare instead of PDO::query.




回答2:


Try

function insertMarker(){
    $lat = $_POST['lat'];
    $long = $_POST['long'];
    $street = $_POST['street'];
    $city = $_POST['city'];
    $zip = $_POST['zip'];

    echo ("$lat, $long, $street, $city, $zip");
    global $dbconnect;
    $query = $dbconnect->prepare("INSERT INTO address (latitude, longitude, street, city, zip) VALUES (?,?,?,?,?)");
    $query->bindParam(1, $lat);
    $query->bindParam(2, $long);
    $query->bindParam(3, $street);
    $query->bindParam(4, $city);
    $query->bindParam(5, $zip);

    $query->execute(array(":lat"=>$lat, ":long"=>$long,":street"=>$street,":city"=>$city,":zip"=>$zip));

}



来源:https://stackoverflow.com/questions/27337984/pdo-error-general-error-2031

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!