I have an SQL query that goes like this:
$stmt = $dbh->prepare("INSERT INTO Places (name, latlng)
VALUES (?, GeomFromText('POINT(? ?)'))");
$stmt->bindValue(1, $_POST['name']);
$stmt->bindValue(2, $_POST['lat']);
$stmt->bindValue(3, $_POST['lng']);
$stmt->execute();
I have been getting this error:
‘PDOException’ with message ‘SQLSTATE[23000]: Integrity constraint
violation: 1048 Column ‘latlng’ cannot be null’
The latlng column is of type point NOT NULL. Since the insert works for the following, why would it cause a NULL to be inserted for the above?
"INSERT INTO Places (name, latlng)
VALUES (?, GeomFromText('POINT(".$_POST['lat']." ".$_POST['lng'].")'))"
So, my question is, can I do a parameterized query containing a geometry function? If so, how? If not, why?
In your query
POINT(? ?)is just a string, not a function. You can’t just parametrize part of a string, you need to parametrize the whole string: