php:将谷歌地图网址放入数据库中

发布于 2024-12-12 02:23:29 字数 1216 浏览 0 评论 0原文

我有一个表单字段,有人可以在其中发布谷歌地图(html)网址,然后我想将其放入数据库中。然后我检索它以直接在我的页面上使用 html 嵌入地图。我尝试过使用 urlencode() 和 htmlspecchars() 但是:

a) 我不确定 $_POST 是否首先错误地处理了数据 b) 我不确定在 mySQL 中存储这样的长 url 的最佳方法

数据库条目很好,它可以进入,但不是全部。不知道哪里被砍断了。我的 db col 是 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 url 约为 1134 个字符,但这就是我得到的全部:

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

非常感谢任何帮助...谢谢。

I have a form field where someone can post a google maps (html) url which I then want to put into the database. Then I am retrieving it to use the html embedded map directly on my page. I have tried using urlencode() and htmlspecchars() but:

a) I'm not sure if $_POST is mishandling the data in the first place
b) I'm not sure the best way to store a long url like this in mySQL

The database entry is fine, it goes in, but not all of it. Not sure where it's getting chopped up. My db col is VARCHAR set to 4000.

html:

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

php database entry:

$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 database retrieval:

$result = '';

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

Roughly, a gmaps url is around 1134 chars but this is all I get back out:

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

Any help much appreciated...thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2024-12-19 02:23:29

您不使用 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
}

You don't use URL/HTML operations to prepare a string to database operations. That's like using a kitten to hammer in a nail.

use mysql_real_escape_string() - that's the right tool. Anything else is a pointless waste of time.

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

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

As well, never EVER assume a database operation has succeeded. You must ALWAYS check return values:

$result = mysql_query($sql);
if ($result === FALSE) {
    die(mysql_error()); // you'll want something better for when this goes into production
}
烟雨扶苏 2024-12-19 02:23:29

我通过使用 PHP 重新生成所有地图代码解决了这个问题,然后将其添加到存储在数据库中的用户搜索词中。这比存储整个链接要好,因为在 PHP 中准备起来既不必要又麻烦。

这也意味着用户不需要获取和发布整个谷歌地图链接。我只是向他们询问位置,然后尝试使用 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 .= "&near=" . $city;    

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

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

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

// print it all out
echo $output;

I solved this by using PHP to regenerate all the map code, and just added in the user's search terms stored in the database. This is better than storing the entire link which is unnecessary and cumbersome to prepare in PHP anyway.

This also means the user does not need to get and post an entire google maps link. I just ask them for a location, then attempt to refine it using the googlemaps query strings (http://querystring.org/google-maps-query-string-parameters/).

Final output code is:

// 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 .= "&near=" . $city;    

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

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

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

// print it all out
echo $output;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文