1

我是一个新手,试图使用数据库为画廊编写 PHP 脚本。我最近更改了脚本和数据库,以便画廊将基于 'photo_caption. 画廊有效,但如果数据库包含引号(例如标题 Nature's Garden And Colors ),脚本只会拉出引号之前的部分(所以这里只有 Nature 一词而不是整个标题 Nature's Garden And Colors ),从而破坏了链接. 现在链接结构是这样/viewgallery.php?cname=Colorado%20Journies&pcaption=Nature'sGardenAndColors的,但由于问题,它显示为/viewgallery.php?cname=Colorado%20Journies&pcaption=Nature

有没有解决这个问题的方法?我怎样才能使 $_GET 语句提取完整的 photo_caption 包括引号?谢谢你的帮助...

我现在使用此代码获取链接的图片说明

$pcaption = isset($_GET['pcaption']) ? ($_GET['pcaption']) : 0;

如果($pcaption){

    $result = mysql_query( "SELECT photo_caption, photo_description, photo_filename,photo_keywords FROM gallery_photos WHERE photo_caption='".addslashes($pcaption)."'" ); 

    list($photo_caption, $photo_description, $photo_filename, $photo_keywords) = mysql_fetch_array( $result ); 

    $nr = mysql_num_rows( $result ); 
     mysql_free_result( $result );     

    $p_caption = $photo_caption;
    $p_description = $photo_description;
    $p_keywords = $photo_keywords;

    //fill pid_array with sorted pids in current category 

    $result = mysql_query( "SELECT photo_caption FROM gallery_photos WHERE category_name='".addslashes($cname)."' ORDER BY photo_caption" ); 

    $ct = mysql_num_rows( $result );     

    while ($row = mysql_fetch_array($result)) { 

          $pid_array[] = $row[0]; 
    } 
    mysql_free_result( $result ); 

    if( empty($nr ) ) 
    { 
        print "%%%%NR is $nr";
        $result_final = "\t<tr><td>***No Photo found</td></tr>\n"; 
    } 
    else 
    { 
        $result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_name='".addslashes($cname)."'" ); 
        list($category_name) = mysql_fetch_array( $result ); 
        mysql_free_result( $result );     
        $result_final = "
        <div class=limagePage>
        <div class=llink><a href='viewgallery.php'>ALBUMS</a><span class=arrow>&gt;&gt</span><a href='viewgallery.php?cname=$cname>$category_name'</a></div>
         ";
        // display previous and next links if more than one photo 

        if ($ct > 1) 
        { 

            $key = array_search($pcaption , $pid_array); 
            $prev = $key - 1; 

            if ($prev < 0) $prev = $ct - 1; 
            $next = $key + 1; 

            if ($next == $ct) $next = 0; 

            //$cname = str_replace(" ","_",$cname);
            //$pcaption=str_replace(" ","_",$pcaption);

            $result_final .= "<div class='prevnext'>"; 
            $result_final .= "<span class='prev'><a href='viewgallery.php?cname=$cname&pcaption=".$pid_array[$next]."'><img src='photos/assets/left.png'  border='0' ></a></span>"; 
            $result_final .= "<span class='next'><a href='viewgallery.php?cname=$cname&pcaption=".$pid_array[$prev]."'><img src='photos/assets/right.png'  border='0' ></a></span>"; 
            $result_final .= "</div>";

        }            
    }
    //$cname = str_replace(" ","_",$cname);
    //$pcaption=str_replace(" ","_",$pcaption);
   $result_final .= "<div class=limage><table><tr><td><table class=image><tr>\n\t<td><a href='viewgallery.php?cname=$cname&pcaption=".$pid_array[$next]."'><img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_keywords."' /></a>
  <div class=caption>".$photo_caption."</div> 
  <div class='excerpt'>".$photo_description."</div> 
  </td>                    
  </tr></table></td></tr></table><div class=underline></div></div>
  <!-- .limagePage --></div>    ";
4

6 回答 6

3

尝试对值进行 url 编码

urlencode($pid_array[$next])

http://php.net/manual/en/function.urlencode.php

于 2013-07-02T01:05:50.260 回答
1

不幸的是urlencode不会影响'尝试,str_replace("'", "%27", $pid_array[$next])也许它会有所帮助。例如,它适用于地址栏中的 Google。

UPD rawurldecode(upd.正确:rawurlencode当然)确实可以做到,反正链接看起来有点难看,充满了%53、%27等。但是urldecode($_GET['pcaption'])在使用它之前不要忘记

于 2013-07-02T03:07:49.777 回答
0
rawurlencode($pid_array[$next])

should also do the trick.

note, using variable parsing quotes uses a bit more processing. since you're already doing it halfway..

$result_final .= "<span class='prev'><a href='viewgallery.php?cname=$cname&pcaption=".$pid_array[$next]."'><img src='photos/assets/left.png'  border='0' ></a></span>"; 

could be swapped for:

$result_final .= '<span class="prev"><a href="viewgallery.php?cname='.urlencode($cname).'&amp;pcaption='.urlencode($pid_array[$next]).'"><img src="photos/assets/left.png" border="0" ></a></span>'; 

also & isn't html valid, &amp; now html valid, quotes dont matter and more efficient cpuwise.

于 2013-07-02T03:01:24.647 回答
0

您需要在链接中打印之前对 url 进行编码,如下所示:

<a href='viewgallery.php?cname=$cname&pcaption=".urlencode($pid_array[$next])."'>

否则认为链接将被提前切断,因为链接将如下所示:

<a href='viewgallery.php?cname=$cname&pcaption=Nature's photo'>
                                                     ^ ends the link
于 2013-07-02T01:08:22.137 回答
0

使用 http_build_query()。使用此函数,您可以将数组转换为 http 查询,例如:
$data = array('name' => 'my name', 'city' => 'my city');

$url = " http://your_url.com/ ?" . http_build_query($data);



输出将类似于:http: //your_url.com/?name=my%20name&city= my%20city

于 2013-07-02T02:54:12.653 回答
0

每个试图帮助我的人..我要感谢大家的帮助。我发现除了数据库中的引号“空格”之外,photo_caption 也会导致问题。所以我尝试修剪代码中的空格(它有效)并决定暂时避免使用引号。只是想留下一个后续说明...谢谢大家。

于 2013-07-02T06:16:35.057 回答