0

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
jjsix
  • 9
  • 1
  • are the parameters being sent correctly on the ajax request. You may want to put a breakpoint to see if position.coords.latitude and position.coords.longitude have values. – It Grunt Mar 19 '20 at 03:56
  • 2
    You are open to SQL injections. Your application should not use `root` and the `root` user should have a password. – user3783243 Mar 19 '20 at 03:57
  • @It Grunt How would I add a breakpoint? – jjsix Mar 19 '20 at 03:58
  • 1
    You don't need a breakpoint. Open the developer console, go to the network tab, and watch the ajax request that gets sent. Go to `form data`. – user3783243 Mar 19 '20 at 04:00
  • @user3783243 Okay, I checked Network/Form Data and I can see that it has the coordinates. What now? What's stopping them from being sent to the database? – jjsix Mar 19 '20 at 04:02
  • Please add the values. Also add what `print_r($_POST)` gives. – user3783243 Mar 19 '20 at 04:04
  • That's what I'm confused about? To clarify, the coordinates will be different depending on who's accessing the page, so I want it to store any coordinates that it gets from the users. That's why the script defines 'lat' and 'lng' as ```position.coords.latitude, position.coords.longitude``` – jjsix Mar 19 '20 at 04:07
  • 1
    When you say _"Upon running it, I receive the error message on the PHP page"_, what **exactly** do you mean there? Are you navigating to `http://localhost/data.php` in your browser? – Phil Mar 19 '20 at 04:09
  • @Phil Yes, I am. – jjsix Mar 19 '20 at 04:10
  • 1
    If you open `data.php` in your browser, that is a GET request; there is no `$_POST` data. Your AJAX request is doing the right thing though and where you have `alert(result)`, if everything worked, you should see an alert box with _"Records inserted successfully."_ – Phil Mar 19 '20 at 04:14
  • Then what would I change?? I only have it running in Wamp, it's just open so I can see the script but there's nothing else. I don't see how I would change the file so it can pass the variables into MySQL. – jjsix Mar 19 '20 at 04:20

0 Answers0