2016-09-06 50 views
0

我遇到了一个问题。如果可能的话,我可以为$ _GET ['topic']编写一个else语句吗?

case 'thread-view': 
     if(isset($_GET['topic'])){ 
     $topicid = $_GET['topic']; 

     $psql = "SELECT * FROM posts WHERE th_p_link='$topicid' ORDER BY p_id ASC"; 
     $presult = mysqli_query($db,$psql); 
     while($prow = mysqli_fetch_array($presult)){ 

      $post = $prow['p_post']; 
      $author = $prow['p_author']; 
      $avatar = $prow['p_avatar']; 
      $gm = $prow['p_gm']; 
      $date = $prow['p_date']; 
      $link = $prow['th_p_link']; 

     echo ' 
     <div class="post_box"> 
      <div id="post-left"> 
       <img src="'.$avatar.'" alt=""> 
       <div id="post-about"> 
        <span>'.$author.'</span> 
        <span>Member</span> 
       </div> 
      </div> 
      <div id="post-content"> 
      <p>'.htmlspecialchars($post, ENT_QUOTES).'</p> 
      </div> 
      <div id="post-right"> 
      <i>'.$date.'</i> 
      </div> 
     </div> 
     '; 
     } 
     } 
    break; 

    default: 
    include 'template/forum_categories.php'; 

举个例子,如果我去的URL

forums.php?网页=线程视图&话题= testurlpost

它正确地显示页面。虽然我想要实现的是,如果我去的东西,不存在如

forums.php?网页=线程视图&话题= testurlthatdoesntexist

我想它重新导向到另一个页面,例如错误页面。我将如何实现这一目标?我为$ _GET ['topic']做了一个else语句,但是当我输入不存在的URL时,这不起作用。

+0

'/forums.php?topic =';%20DROP%20TABLE%20posts;%20 - ' – CD001

+0

是否有错误,这就是为什么它没有工作?你有没有检查你的错误日志? –

+0

查询后,检查结果行是否有零行,比id没有存在,所以你可以使用'header('Location:xy.php'); exit;'重定向 – JustOnUnderMillions

回答

0

你应该在语句前加上

if(!mysqli_num_rows($presult)) { 
     header("Location: /error-page.html"); 
    } 

。所以,你的代码将是这个样子:

case 'thread-view': 
    if(isset($_GET['topic'])){ 
    $topicid = $_GET['topic']; 

    $psql = "SELECT * FROM posts WHERE th_p_link='$topicid' ORDER BY p_id ASC"; 
    $presult = mysqli_query($db,$psql); 

    if(!mysqli_num_rows($presult)) { 
     header("Location: /error-page.html"); 
    } 

    while($prow = mysqli_fetch_array($presult)){ 
+0

中的第一个单引号'\''谢谢!我完全忘记了mysqli_num_rows函数 – Synyster