0

I am trying to store longitude and latitude in a mysql table of datatype POINT using following code:-

$name = $_POST['name']
$lat = $_POST['lat'];
$long = $_POST['longi'];
$location = 'POINT('.$lat."".$long.')';
$sql = "insert into `metrix` (`name`, `coord`) values ('".$name.", '".$location."')";
$result = mysqli_query($con,$sql);
if (empty($result)) {
Echo mysqli_error($con);}

and it gives following MySql syntax error:-

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'POINT(13,132)')' at line 1

What is the correct way of doing this?

thank you

3
  • POINT is a function and you do not need to wrap with single quote, it should be as ".$location." Commented Mar 31, 2015 at 7:20
  • Don't use variable substitution in the first place, use a prepared statement. Commented Mar 31, 2015 at 7:22
  • Your code is subject to SQL injection because of this. Commented Mar 31, 2015 at 7:23

3 Answers 3

2

Remove the single quotes as Point is a function

$sql = "insert into `metrix` (`name`, `coord`) values ('".$name."', ".$location.")";

Also I would recommend you to use Prepared statement as your present query is very much prone to SQL Injection.

Sign up to request clarification or add additional context in comments.

Comments

1

You miss an single qoute after ".$name.":

$sql = "insert into `metrix` (`name`, `coord`) values ('".$name."', ".$location.")";

and remove the single quotes arround ".$location." because POINTis a function and not a string.

And you should use prepared statements.

Comments

0

Remove single quote from location because POINT is function and Put single quote before and after name.

$sql = "insert into `metrix` (`name`, `coord`) values ('".$name."', ".$location.")";

Comments

Your Answer

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.