PDO错误:一般错误:2031

时间:2014-12-06 23:59:47

标签: php pdo

我在执行代码时遇到此错误。 我知道这已在这里多次讨论过,但我无法通过阅读那里提供的解决方案来解决我的问题。

这是我得到的错误: 致命错误:带有消息'SQLSTATE [HY000]的未捕获异常'PDOException':常规错误:2031

这是我的代码:

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));
}

2 个答案:

答案 0 :(得分:3)

让我的评论回答:

  

如果dbconnectPDO的实例,那么query both creates a prepared statement and then executes it all in one go。所以它最初没有得到参数绑定。使用PDO::prepare代替PDO::query

答案 1 :(得分:0)

尝试

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));

}