Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert postgis geometry with GET values

Tags:

php

postgresql

I'm trying to make an insert using ST_Makepoint with get values, but I run into 500 Error.

This is my php code:

<?php
try {
    $user = 'user';
    $dbh = new PDO('pgsql:host=localhost;dbname=userdb', $user);


    $stmt = $dbh->prepare("INSERT INTO table(id_a, id_b, geom) VALUES (?,?,?);");

    if ($stmt->execute(array($_GET['id_a'], $_GET['id_b'], ST_SetSRID(ST_MakePoint($_GET['lat'], $_GET['long']),4326)))) {
        print_r("OK");
    } else {
        print_r("Error");
    }

} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?>

If I run this query with pgAdmin, it runs well:

INSERT INTO table(id_a, id_b, geom) VALUES (1,1,ST_SetSRID(ST_MakePoint(2, 2),4326));

Do you know how to fix the problem in php code?

like image 431
Giuseppedes Avatar asked Dec 18 '25 11:12

Giuseppedes


1 Answers

I solved in this way:

$stmt = $dbh->prepare("INSERT INTO table(id_a, id_b, geom) VALUES (?,?,ST_SetSRID(ST_MakePoint(?, ?),4326));");

    if ($stmt->execute(array($_GET['id_a'], $_GET['id_b'], $_GET['lat'], $_GET['long']))) {
        print_r("OK");
    } else {
        print_r("Errore");
    }
like image 65
Giuseppedes Avatar answered Dec 19 '25 23:12

Giuseppedes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!