问题
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
dbconnectis an instance ofPDOthen 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 ofPDO::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