简体   繁体   中英

AJAX inserting NULL values into MYSQL

My HTML script gathers the latitude & longitude and sends them to another PHP script via ajax, which inserts those values into mysql, problem is that AJAX inserts NULL values into the table. It's inserting values of "0.000000" for both Latitude & Longitude, lat & long are type float(10,6) in the DB as stated by the internet for best practice with gps coords. I'm obvious new developer please help -- HTML Script

<!DOCTYPE html>
<html>
  <head>
    <title>AJAX DATABASE</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
    <script type="text/javascript" charset="utf-8">



    /*/ Wait for Cordova to load
    /*/
    document.addEventListener("deviceready", onDeviceReady, false);

    var watchID = null;

    // Cordova is ready
    //

    function onDeviceReady() {
        // Throw an error if no update is received every 30 seconds
        var options = {enableHighAccuracy:true, timeout: 3000, maximumAge:0};
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);

    }


    // onSuccess Geolocation
    //
    function onSuccess(position) {    


        var latitude = document.getElementById("lat");
        latitude.innerHTML = position.coords.latitude;
        var longitude = document.getElementById("longitude");
        latitude.innerHTML = position.coords.latitude;
        insertDB(position.coords.latitude,position.coords.latitude);
    }

    // onError Callback receives a [PositionError](PositionError/positionError.html) object
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    // onError Callback receives a PositionError object
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    // Options: throw an error if no update is received every 30 seconds.
    //
    var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 30000 });




    // Sends latitude & longitude value to php script and alerts on success

    function insertDB(latitude, longitude){
        var xttp = new XMLHttpRequest();
        xttp.onreadystatechange = function(){
            if(this.readyState == 4){
                document.getElementById('longitude').innerHTML = 
                this.responseText;
                alert("state changed by ajax")
            }

        }
        xttp.open("GET", "ConnectTest.php? 
         latitude="+latitude+"&longitude="+longitude, true);
        xttp.send();
    }




    </script>


  </head>
  <body>

    <p id="lat"></p><br>
    <p id="longitude"></p>
  </body>
</html>

and the PHP --

<?php 


    //POST VARIABLES


    $one = $_GET['latitude'];
    $two = $_GET['longitude'];

    //ESTABLISH CONNECTION 

    $servername = "localhost:3306";
    $user = "client";
    $pass = "sonic_client";

    $db_name = "sonicstrains";

    $server = mysqli_connect($servername, $user, $pass, $db_name);




    $sql = "SELECT * FROM 'users' WHERE 'latitude' = ".$two;

    $result = mysqli_query($server, $sql);

    if(!isset($_GET['latitude'])){
        echo "Nothing is set";

    }

    if(!$server){

        die("Connetion error".mysqli_connect_error());

    }else{

        if (!$result){

            $insert = "INSERT INTO users (latitude, longitude) VALUES ('$one', '$two')";
            mysqli_query($server, $insert);
            echo "Coordinates Inserted";

        }
    }


?>

Replace the xttp open and send functions with below:

    xttp.open("POST", "ConnectTest.php, true);
    xttp.send(latitude="+latitude+"&longitude="+longitude);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM