2016-05-29 39 views
0

我想將緯度和經度座標存儲到數據庫。存儲經度和緯度到數據庫的問題

下面的代碼成功地將用戶名寫入數據庫,但對於經度和緯度而言是空白的。

任何想法我做錯了什麼?

感謝

<!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(); 

?> 
+0

你可以給你試圖插入數據的數據庫表的定義嗎? –

回答

0

試試這個:

<!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(); 

?> 

*的形式被回調來與座標之前提交的,所以我改變了提交按鈕正常按鈕,提交回調中的表單。

相關問題