切换$ _GET变量else语句

时间:2016-09-06 15:10:04

标签: php

我遇到了一个问题。如果可能的话,我可以为$ _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时这不起作用。

1 个答案:

答案 0 :(得分:0)

你应该添加

   if(!mysqli_num_rows($presult)) {
       header("Location: /error-page.html");
    }
在While声明之前

。所以你的代码看起来像这样:

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)){