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

时间:2016-03-18 16:57:21

标签: php mysql redirect mysqli url-parameters

我有一个表格,其中一个属性是版本。

表名是版本。

如果URL参数也称为版本为NULL或不存在,并且版本值不在我的表中,我想将用户重定向到默认页面。

到目前为止,我已经这样了。它不起作用。

$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 个答案:

答案 0 :(得分:2)

你必须添加exit();标题功能之后。

尝试类似的事情:

  <?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 :(得分:0)

使用$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'"); $total_num_rows = $result->num_rows; if ($total_num_rows>0) { while ($row = $result->fetch_assoc()) { $varex = $row['version']; } } else{ header("Location: index.php?default"); } 计算结果并查看版本是否存在。

None
相关问题