dpj83664 2016-03-18 16:57
浏览 29
已采纳

基于URL参数重定向,如果URL参数在表行中可用 - PHP MySQL

I have a table where one of the attribute is version.

The table name is versions.

I want to redirect users to a default page if the URL parameter also called version is NULL or not present, and if the version value is not in my table.

So far I've this. It does not work.

$stonevar = isset($_GET['version']) ? $_GET['version'] : NULL;

if (empty($stonevar)) {
header("Location: index.php?version=default");
 }

$id = $_GET['version'];
$result = $db->query("SELECT * FROM `versions` WHERE version='$id'");

 while ($row = $result->fetch_assoc()) {
  $varex = $row['version'];
 }

if ($varex == NULL) {
header("Location: index.php?default");
}
  • 写回答

2条回答 默认 最新

  • dongshai2022 2016-03-18 17:36
    关注

    You have to add exit(); after header function.

    Try Something like:

      <?php
        $stonevar = isset($_GET['version']) ? $_GET['version'] : NULL;
    
        if (empty($stonevar)) {
        header("Location: index.php?version=default");
        exit();
         }
    
        $result = $db->query("SELECT * FROM `versions` WHERE version='$stonevar'");
    
         while ($row = $result->fetch_assoc()) {
    
        header("Location: index.php?".$row['version']);
        exit();
    /*
    * or something you want to do if version exist
    */
         }
    
        header("Location: index.php?default");
        exit();
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?