Php 如何使用$\u GET检查子类别的值是否在类别中?

Php 如何使用$\u GET检查子类别的值是否在类别中?,php,mysqli,get,Php,Mysqli,Get,我正在制作一个论坛系统,以加强我的php,并为我拥有的社区制作一个网站。我不知道如何检查我选择的子类别是否在指定类别内,例如我们有localhost/forum/sub_category.php?cat=1&sub_cat=1我想知道的是,用户是否试图将类别id设置为3,子类别设置为4,子类别id不存在。如何使用$\u GET进行检查,然后向用户显示一条消息,说明指定的子类别不存在。这是我的密码: <?php include '../inc/dbc.php'; include '../i

我正在制作一个论坛系统,以加强我的php,并为我拥有的社区制作一个网站。我不知道如何检查我选择的子类别是否在指定类别内,例如我们有
localhost/forum/sub_category.php?cat=1&sub_cat=1
我想知道的是,用户是否试图将类别id设置为3,子类别设置为4,子类别id不存在。如何使用$\u GET进行检查,然后向用户显示一条消息,说明指定的子类别不存在。这是我的密码:

<?php

include '../inc/dbc.php';
include '../inc/functions.php'
?>
<?php
if(isset($_GET['user']) && !empty($_GET['user'])) {
    $username = $_GET['user'];
} else {
    $username = $_SESSION['username'];
}
    $my_name = $_SESSION['username'];
    $firstname = getuser($username, 'firstname');
    $lastname = getuser($username, 'lastname');
?>
<!DOCTYPE html>
<html>
    <head>
        <title>EWC Forums</title>
        <link rel="stylesheet" type="text/css" href="css/index.css">
    </head>

    <body>
        <?php include 'header.php';?>

        <div class='main'>
            <div class='body'>
                <div class='left'>
                    <?php
                        if (isset($_GET['cat']) && isset($_GET['sub_cat'])) {
                            $cat_query = mysqli_query($mysqli, "SELECT * FROM categories Where");// this is not done yet
                        } else if ($_GET['sub_cat'] == 0) {
                            $msg = 'That Sub Category Was Not Found In Our Database';
                        } else {
                    ?>  
                        <div class='message'><?php echo $msg;?></div>
                    <?php   
                        }
                    ?>
                </div>
                <div class='right'>

                </div>
                <div class='clear'></div>
            </div>
        </div>
    </body>
</html>

EWC论坛
这是显示类别和子类别的主页面:

<?php

include '../inc/dbc.php';
include '../inc/functions.php'
?>
<?php
if(isset($_GET['user']) && !empty($_GET['user'])) {
    $username = $_GET['user'];
} else {
    $username = $_SESSION['username'];
}
    $my_name = $_SESSION['username'];
    $firstname = getuser($username, 'firstname');
    $lastname = getuser($username, 'lastname');
?>
<!DOCTYPE html>
<html>
    <head>
        <title>EWC Forums</title>
        <link rel="stylesheet" type="text/css" href="css/index.css">
    </head>

    <body>
        <?php include 'header.php';?>

        <div class='main'>
            <div class='body'>
            <?php
                if(loggedIn() == false) {
            ?>
                <div class='mbli'>You Are Not Logged In Right Now. Click Here To <a href="login.php">Login</a></div><br>
            <?php
                }
            ?>
                <div class='forum'>
                <div class='tools'>
                    <?php
                        $sql = mysqli_query($mysqli, "SELECT * FROM users WHERE rank = 'Administrator' ");
                        $res = mysqli_num_rows($sql);
                        if ($sql == 1) {
                    ?>
                        <div class='forummenu'>
                            <a href="create_category.php"><i class=''></i> New Category</a>
                            <a href="create_subcategory.php"><i class=''></i> New Sub Category</a>
                        </div>
                    <?php
                        }
                    ?>
                </div>
                    <?php
                        $check_forum = mysqli_query($mysqli, "SELECT * FROM categories");
                        while ($run = mysqli_fetch_array($check_forum)) {
                            $cat_id = $run['cat_id'];
                            $creator = $run['creator'];
                            $cat_name = $run['name'];
                            $create_date = $run['create_date'];
                            $activated = $run['activated'];
                    ?>
                    <div class='cat'>
                        <style type="text/css">
                        .tg  {border-collapse:collapse;border-spacing:0;}
                        .tg td{font-family:Arial, sans-serif;font-size:14px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
                        .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;border-style:solid;border-width:1px;overflow:hidden;}
                        .tg .tg-7ht0{background-color:#f07075;color:#ffffff}
                        </style>
                        <table>
                          <tr class="cat_head">
                            <th class="tg-7ht0"><b><?php echo $cat_name;?></b><i><?php echo $create_date;?></i></th>
                            <th class="tg-7ht0">Threads</th>
                            <th class="tg-7ht0">Posts</th>
                            <th class="tg-7ht0">Latest Posts</th>
                          </tr>
                    <?php
                            $check_subcat = mysqli_query($mysqli, "SELECT * FROM subcategories WHERE cat_id= '$cat_id' ");
                            while ($run_sub = mysqli_fetch_array($check_subcat)) {
                                $sub_cat_id = $run_sub['sub_cat_id'];
                                $creator_subcat = $run_sub['creator'];
                                $name_sub = $run_sub['name'];
                                $sub_desc = $run_sub['sub_desc'];
                                $create_date_sub = $run_sub['create_date'];
                    ?>
                              <tr class='sub_cat'>
                                <td class="tg-7ht0"><div class='sub_img'><img src="http://minotar.net/helm/<?php echo $creator_subcat;?>/32"></div><div class='sub_body'><a href='sub_category.php?cat=<?php echo $cat_id;?>&sub_cat=<?php echo $name_sub;?>'><div class='sub_tit'><b><?php echo $name_sub; ?></b><div class='desc'><?php echo $sub_desc;?></div></div></a><i class='date'><?php echo $create_date_sub; ?></i></div></td>
                                <td class="tg-7ht0">0</td>
                                <td class="tg-7ht0">0</td>
                                <td class="tg-7ht0">Nub picture</td>
                              </tr>
                    <?php
                            }

                    ?>
                              <div class='clear'></div>
                            </table>
                        </div>
                    <?php
                        }
                    ?>
                </div>
            </div>
        </div>
    </body>
</html>

EWC论坛
您现在没有登录。单击此处查看
尝试使用此选项:
编辑:带有评论的子类别屏幕截图:

<?php
    if (isset($_GET['cat']) && isset($_GET['sub_cat'])) {
        $cat_query = mysqli_query($mysqli, "SELECT * FROM SubCategoryTableName WHERE sub_cat_id='".$_GET['sub_cat']."' AND cat_id ='".$_GET['cat']."'");// this is not done yet

        // Check if number of ROWS are zero/sub category notfound! .

        if(mysqli_num_rows($cat_query)) == 0)
        {
            // show error and die
            die('The requested sub/category not found.');
        }

    } else if ($_GET['sub_cat'] == 0) {
        $msg = 'That Sub Category Was Not Found In Our Database';
    } else {
?>  


请注意,此代码是有效的,但必须始终确保用户请求的输入。

我不知道您的id是否会自动向上移动,否则您只需输入一条检查语句,查看cat和sub cat的编号是否正确,是否过高或过低

如果您确实依赖于您的数据库,那么您可以要求查询查看它是否存在,或者要求它提供数据库中的最新id,然后使用该id检查您的id是否低于该id


我希望我在这里正确理解了你的问题。别忘了验证GET请求(顺便说一句;)

谢谢你们的帮助,因为你们,我找到了解决问题的方法,这可能不是正确的方法,但已经很接近了。这是我的密码:

<?php

include '../inc/dbc.php';
include '../inc/functions.php'
?>
<?php
if(isset($_GET['user']) && !empty($_GET['user'])) {
    $username = $_GET['user'];
} else {
    $username = $_SESSION['username'];
}
    $my_name = $_SESSION['username'];
    $firstname = getuser($username, 'firstname');
    $lastname = getuser($username, 'lastname');
?>
<!DOCTYPE html>
<html>
    <head>
        <title>EWC Forums</title>
        <link rel="stylesheet" type="text/css" href="css/index.css">
    </head>

    <body>
        <?php include 'header.php';?>

        <div class='main'>
            <div class='body'>
                    <?php
                        if(isset($_GET['cat']) && isset($_GET['sub_cat'])) {
                        $sub = $_GET['sub_cat'];
                        $cat = $_GET['cat'];
                        $sql = mysqli_query($mysqli, "SELECT * FROM subcategories WHERE sub_cat_id = '$sub' AND cat_id = '$cat' ");
                        if (!$sql) {
                            $msg = 'Sorry We Kept Looking, But We Found Nothing ;-;';
                        } else {
                            if (mysqli_num_rows($sql) == 0) {
                                $msg = 'Ask An Admin';
                            } else {
                        ?>
                            <?php
                                while ($row = mysqli_fetch_array($sql)) {
                            ?>


                            <div class='cat'>
                                <style type="text/css">
                                .tg  {border-collapse:collapse;border-spacing:0;}
                                .tg td{font-family:Arial, sans-serif;font-size:14px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
                                .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;border-style:solid;border-width:1px;overflow:hidden;}
                                .tg .tg-7ht0{background-color:#f07075;color:#ffffff}
                                </style>
                                <table>
                                  <tr class="cat_head">
                                    <th class="tg-7ht0"><b><?php echo $row['name'];?></b><i><?php echo $row['create_date'];?></i></th>
                                    <th class="tg-7ht0">Views</th>
                                    <th class="tg-7ht0">Replies</th>
                                    <th class="tg-7ht0">Latest Posts</th>
                                  </tr>
                                    <?php
                                        $thread_query = mysqli_query($mysqli, "SELECT * FROM threads WHERE sub_cat_id = $sub ");
                                        while ($run = mysqli_fetch_array($thread_query)) {
                                            $thread_id = $run['thread'];
                                            $thread_author = $run['creator'];
                                            $thread_title = $run['name'];
                                            $thread_date = $run['create_date'];
                                    ?>
                                      <tr class='sub_cat'>
                                <td class="tg-7ht0"><div class='sub_img'><img src="http://minotar.net/helm/<?php echo $thread_author; ?>/32"></div><div class='sub_body'><a href=''><b><?php echo $thread_title; ?></b></a >&nbsp;created by <a href='profile.php?user=<?php echo $thread_author;?>'><?php echo $thread_author;?></a> at <?php echo $thread_date; ?></div></td>
                                <td class="tg-7ht0">0</td>
                                <td class="tg-7ht0">0</td>
                                <td class="tg-7ht0">Nub picture</td>
                              </tr>
                                    <?php
                                        }
                                }
                            ?>
                                        <div class='clear'></div>
                                    </table>
                                </div>
                            <?php
                            }
                        }
                    } else {
                    ?>
                        <div class='message'><?php echo $msg; ?></div>
                    <?php
                    }
                    ?>
                <div class='clear'></div>
            </div>
        </div>
    </body>
</html>

EWC论坛

在深入编写代码之前,您应该验证数据库中的类别和子类别。您忘记在页面顶部初始化会话。会话是在functions.php文件中初始化的。您实际上不需要知道子类别的类别。如果db设置正确,则子类别有一个
cat\u id
如果您想输出上层类别的名称,您将使用该id在
categories
表中进行检查,并显示其名称……我添加了更多内容code@ClementAbel,答案用子类别结构截图编辑,但是我不知道子类别表名,然后用你的重新绘制
子类别表名
。哦,好的,它被称为
子类别
,它向我显示了一个没有错误的空白页,我想这与引号有关。@ClementAbel,是的,还有一个引号正在查询中。使用此答案的新查询。如果重新使用,则在第一次编辑后,也不会包含第二个引号。