So I have two issues, they seem relatively simple but I cannot figure them out. I am working on an application that takes latitude and longitude from a javascript script, uses a POST method to send the latitude and longitude to my PHP file, where it will be sent to MySQL database.
Javascript:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$.ajax({
url: 'http://localhost/data.php',
data: {
'lat': position.coords.latitude,
'lng': position.coords.longitude
},
type: 'POST',
success: function (result) {
// If your backend page sends something back
alert(result);
}
});
// Use the 2 lines below to center your map on user location (you need a map instance at this point)
userLoc = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.panTo(userLoc);
});
}
PHP:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$conn = mysqli_connect("localhost:3306", "root", "", "coordinatedatabase");
// Check connection
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt insert query execution
$sql = "INSERT INTO coordinatedatabase (latitude_value, longitude_value) VALUES ({$_POST['lat']}, {$_POST['lng']})";
if(mysqli_query($conn, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
Upon running it, I receive the error message on the PHP page:
Undefined index: lat in C:\wamp64\www\data.php on line 16
Undefined index: lng in C:\wamp64\www\data.php on line 16
ERROR: Could not able to execute INSERT INTO coordinatedatabase (latitude_value, longitude_value) VALUES (, ). You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' )' at line 1