2011-10-24 84 views
0

我有一個表單字段,有人可以發佈一個谷歌地圖(html)url,然後我想把它放到數據庫中。然後我檢索它直接在我的頁面上使用html嵌入式地圖。我嘗試過使用urlencode()和htmlspecchars(),但是:php:把谷歌地圖網址放在數據庫中

a)我不確定是否$ _POST在第一個位置處理數據的操作不當 b)我不確定存儲長時間這樣的網址在mySQL

數據庫條目很好,它進去,但不是全部。不知道它在哪裏被切碎。我的數據庫山坳被VARCHAR設置爲4000

HTML:

<p class="form-title">Google map link</p> 
<textarea id="map_link" cols="100" rows="5" name="maplink_entry"></textarea> 

PHP數據庫條目:

$map_link_entry = $_POST['map_link_entry']; 
$safe_map_link_entry = mysql_real_escape_string($map_link_entry); 
$query_do_entries = mysql_query("INSERT INTO all_places VALUES ('',(NOW()), '$address_entry','$safe_map_link_entry', '$username_entry', '$like', '$dislike', '$source')"); 

PHP數據庫檢索:

$result = ''; 

while ($row = mysql_fetch_assoc($query)) { 
    $result .= '<li>';  
    $result .= stripslashes($row['map_link']); 
    $result .= '</li>'; 
} 

粗略地說,一個gmaps網址是大約1134個字符,但這是所有我回來了:

<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q 

任何幫助非常感謝......謝謝。

回答

1

我解決了這個用PHP來重新生成所有地圖代碼,只是存儲在數據庫中的用戶的搜索字詞添加。無論如何,這比存儲整個不需要和繁瑣的鏈接都要好。

這也意味着用戶不需要獲取併發布完整的Google地圖鏈接。我只是問他們一個位置,然後嘗試使用googlemaps查詢字符串(http://querystring.org/google-maps-query-string-parameters/)對其進行優化。

最終輸出代碼是:

// set up variables 
$output = ''; 
$map_width = '600';  // map frame width 
$map_height = '350'; // map frame height 

while ($row = mysql_fetch_assoc($query)) { 

    $location = stripslashes($row['location']); 
    $city = stripslashes($row['city']); 

    // compile resulting html with variables and db results 
    $output .= "<iframe width='" . 
       $map_width . "' height='" . $map_height . 
       "' frameborder='0' scrolling='no' 
       marginheight='0' marginwidth='0' src='"; 

    // the original search query (googlemaps api uses "q=...")   
    $output .= "http://maps.google.com/maps?q=" . $location; 

    // location to refine the query (googlemaps api uses "near=...") 
    $output .= "&amp;near=" . $city;  

    // set map to 'terrain'      
    $output .= "&amp;t=p"; 

    //zoom level    
    $output .= "&amp;z=15";       
    $output .= "&amp;output=embed'></iframe>"; 

    $output .= "<br /><small><a href='" . $location . 
       "&amp;output=source' target='_blank' 
       style='color:#0000FF;text-align:left'> 
       View Larger Map</a></small>"; 
} 

// print it all out 
echo $output; 
2

您不使用URL/HTML操作爲數據庫操作準備字符串。這就像使用小貓敲釘子一樣。使用mysql_real_escape_string() - 這是正確的工具。其他任何東西都是毫無意義的浪費時間。

$url = $_POST['...']; 
$safe_url = mysql_real_escape_string($url); 

$sql = "INSERT ... VALUES ('$safe_url');"; 

同樣,永遠不會假設數據庫操作成功。你必須經常檢查返回值:

$result = mysql_query($sql); 
if ($result === FALSE) { 
    die(mysql_error()); // you'll want something better for when this goes into production 
} 
+0

嘿馬克。謝謝你的支持者,我會試試看。 (我正在測試數據庫條目,只是沒有粘貼在這裏...) – matski

+0

大聲笑,我碰巧讀了你的答案(在不同的問題)2緊接着,我突然被你使用的真棒圖像(西葫蘆,小貓)。你很棒,男人! –

+0

你是我的英雄! – daniyalahmad