将经度和纬度存储到数据库时出现问题



我正在尝试将纬度和经度坐标存储到数据库中。

下面的代码成功地将用户名写入数据库,但经度和纬度为空。

知道我做错了什么吗?

谢谢

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title>Locate skills Reg</title>
  <style>
#header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px;
}
#side {
    line-height:30px;
    background-color:white;
    height:500px;
    width:250px;
    float:left;
    padding:5px;          
}
#main {
    width:350px;
    float:left;
    padding:10px;        
}
#aside {
    width:250px;
    float:right;
    padding:5px;
}
#footer {
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
   padding:5px;      
}
</style>
 </head>
 <body>
 <div id="header">
  <h1>Location/Skill Register</h1>
  <?php
                    session_start();
                    $username = $_SESSION['username'];
                    if($_SESSION['name'])
                    {
                        echo "Welcome, <a href='userProfile.php'>" .$_SESSION['login']."
                        &nbsp</a>&nbsp | <a href = 'logout.php'>Log Out!</a>";
                    }
                    else
                    {
                        echo '<script type="text/javascript">'; 
                        echo 'alert("You must be logged in to do that!");'; 
                        echo 'window.location.href = "login.html";';
                        echo '</script>';
                        //echo "<a href = 'login.html'>Login</a> | <a href = 'register.php'>Register</a>";
                    }
                    ?>
 </div>
<br>
<div id="side">
<a href="homepage.php">
<img src="logo.png" alt="logo" style="width:200px;height:200px;border:0;"><br>
Donate an Item Here<br>
<a href = "alldonations.php">List of donations<br>
<a href = "requestList.php">Request for Items</a><br>
List of requested Items<br>
My Account Posts<br>
<a href = "feedback.php">Feedback</a><br>
<br>
</div>
<div id="main">
<table frame="border"  valign="center" align ="center">         
  <form action ="locateskills.php" method ="POST" >
        <td><input type="hidden" id = "latitude"  name="latitude"/></td>                   
        <td><input type="hidden" id="longitude" name="longitude"/></td>
        <tr><td>Please enter description of skill your are providing or requesting for: </br>
        <textarea rows="4" cols="50" name = "description"></textarea></td></tr>
       <br />
       </td><td align="right"><input type= "submit" onclick="getLocation()" name = "submit" value="submit"></td></tr>
                </b></font>
</form>
</table>
       <br/>
<Script>
function getLocation() 
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    } 
    else 
    {
        alert("Geolocation Failed!");
    }
}
function showPosition(position) 
{
      x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
  latitude = position.coords.latitude;
  longitude = position.coords.longitude;
  document.getElementById("latitude").value = String(latitude);
  document.getElementById("longitude").value = String(longitude);
}
</Script>
</body>
</html>
<?php
session_start();
$submit = $_POST['submit'];
$username = $_SESSION['login'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$connect = mysql_connect('HOST', 'USER', 'PASS') or die('Failed to connect!');
mysql_select_db('DB') or die('Can't find Database!');
if($submit)
{                   
    mysql_query("INSERT INTO LOCATIONS VALUES ('$username', '$latitude', '$longitude')");
    echo '<script type="text/javascript">'; 
    echo 'alert("Stored to Database");'; 
    echo 'window.location.href = "homepage.php";';
    echo '</script>';
}

//close once finished to free up resources
  $db->close();
?>

试试这个:

    <!DOCTYPE html>
<html>
 <head>
  <title>Locate skills Reg</title>
  <style>
#header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px;
}
#side {
    line-height:30px;
    background-color:white;
    height:500px;
    width:250px;
    float:left;
    padding:5px;          
}
#main {
    width:350px;
    float:left;
    padding:10px;        
}
#aside {
    width:250px;
    float:right;
    padding:5px;
}
#footer {
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
   padding:5px;      
}
</style>
 </head>
 <body>
 <div id="header">
  <h1>Location/Skill Register</h1>
  <?php
                    session_start();
                    $username = $_SESSION['username'];
                    if($_SESSION['name'])
                    {
                        echo "Welcome, <a href='userProfile.php'>" .$_SESSION['login']."
                        &nbsp</a>&nbsp | <a href = 'logout.php'>Log Out!</a>";
                    }
                    else
                    {
                        echo '<script type="text/javascript">'; 
                        echo 'alert("You must be logged in to do that!");'; 
                        echo 'window.location.href = "login.html";';
                        echo '</script>';
                        //echo "<a href = 'login.html'>Login</a> | <a href = 'register.php'>Register</a>";
                    }
                    ?>
 </div>
<br>
<div id="side">
<a href="homepage.php">
<img src="logo.png" alt="logo" style="width:200px;height:200px;border:0;"><br>
Donate an Item Here<br>
<a href = "alldonations.php">List of donations<br>
<a href = "requestList.php">Request for Items</a><br>
List of requested Items<br>
My Account Posts<br>
<a href = "feedback.php">Feedback</a><br>
<br>
</div>
<div id="main">
<table frame="border"  valign="center" align ="center">         
  <form action ="locateskills.php" method ="POST" id="locationForm">
        <td><input type="hidden" id = "latitude"  name="latitude"/></td>                   
        <td><input type="hidden" id="longitude" name="longitude"/></td>
        <tr><td>Please enter description of skill your are providing or requesting for: </br>
        <textarea rows="4" cols="50" name = "description"></textarea></td></tr>
       <br />
       </td><td align="right"><input type="button" onclick="getLocation()" value="submit"></td></tr>
                </b></font>
</form>
</table>
       <br/>
<script>
function getLocation() 
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    } 
    else 
    {
        alert("Geolocation Failed!");
    }
}
function showPosition(position) 
{
    //   x.innerHTML = "Latitude: " + position.coords.latitude + 
    // "<br>Longitude: " + position.coords.longitude;
  latitude = position.coords.latitude;
  longitude = position.coords.longitude;
  document.getElementById("latitude").value = String(latitude);
  document.getElementById("longitude").value = String(longitude);
  document.getElementById("locationForm").submit();
}
</script>
</body>
</html>
<?php
session_start();
$submit = $_POST['submit'];
$username = $_SESSION['login'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$connect = mysql_connect('HOST', 'USER', 'PASS') or die('Failed to connect!');
mysql_select_db('DB') or die('Can't find Database!');
if($submit)
{                   
    mysql_query("INSERT INTO LOCATIONS VALUES ('$username', '$latitude', '$longitude')");
    echo '<script type="text/javascript">'; 
    echo 'alert("Stored to Database");'; 
    echo 'window.location.href = "homepage.php";';
    echo '</script>';
}

//close once finished to free up resources
  $db->close();
?>

*表单是在回调附带坐标之前提交的,因此我将提交按钮更改为普通按钮并在回调中提交了表单。

最新更新