将经度和纬度存储到数据库的问题
Posted
技术标签:
【中文标题】将经度和纬度存储到数据库的问题【英文标题】:Problems with storing longitude and latitude to databse 【发布时间】:2016-05-29 04:25:54 【问题描述】:我正在尝试将经纬度坐标存储到数据库中。
以下代码成功将用户名写入数据库,但经纬度为空。
任何想法我做错了什么?
谢谢
<!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']."
 </a>  | <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" 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();
?>
【问题讨论】:
你能给出你试图插入数据的数据库表的定义吗? 【参考方案1】:试试这个:
<!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']."
 </a>  | <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" 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();
?>
*在回调带有坐标之前提交表单,所以我将提交按钮更改为普通按钮并在回调中提交表单。
【讨论】:
以上是关于将经度和纬度存储到数据库的问题的主要内容,如果未能解决你的问题,请参考以下文章
需要使用 PHP 将 android app 生成的纬度和经度值存储到 mySQL 数据库中