1

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 Answers 2

2

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();
    ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Use $result->num_rows to count the result and see if the version exists.

$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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.