1

I have a problem with sql insert statement using PDO (php, postgis, postgres). I have tried to use different combinations, but none of them work. Here is what I ve got:

$latitude= 46.07175;
$longitude=16.866976;
...
$sql = "insert into table (geom, name, type) values ('(st_setsrid(st_makepoint('|| $longitude ||',' || $latitude || '),4326))'::geom, :name, :type");
Evan Carroll
7,2592 gold badges34 silver badges67 bronze badges
asked Jun 21, 2016 at 16:47
1
  • You are mixing " and ' in a very weird way. When you start concatenating strings with || you need to end the existing string input with whatever you started with, in this case ". Commented Jun 21, 2016 at 17:55

2 Answers 2

2

Other than your quotes being funky, if you're using PDO. Use placeholders all the way through. I prefer the ? and the array().

/* Execute a prepared statement by passing an array of values */
$sth = $dbh->prepare('
 INSERT INTO myTable (geom, name, type) VALUES
 ( ST_SetSRID(ST_Makepoint(?, ?), 4326), ?, ? );
');
$sth->execute(array( $longitude, $latitude, $name, $type ));

This has the advantage of

  1. caching the plan, and not having to replan the query for each insert
  2. not having to escape the $long and $lat or worry about sql injection attacks

As a side note if you're using 4326 you should consider the geography type.

answered Oct 31, 2017 at 20:29
1

Try this:

$sql = "insert into table (geom, name, type) values (st_setsrid(
st_makepoint($longitude, $latitude), 4326))";

In double quotes variable substitution is made by PHP, || is the SQL string concatenation operator.

answered Jun 21, 2016 at 18:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.