ajax将空值插入mysql

6jygbczu  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(359)

我的html脚本收集经纬度并通过ajax将它们发送到另一个php脚本,该脚本将这些值插入mysql,问题是ajax将空值插入表中。它为纬度和经度,纬度和经度都插入了“0.000000”的值,在数据库中输入float(10,6),这是互联网为gps坐标的最佳实践所规定的。我是一个新的开发人员请帮助-html脚本

<!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>

还有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";

        }
    }

?>
fdx2calv

fdx2calv1#

将xttp open和send函数替换为:

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

相关问题