Reputation: 1835
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");
}
Upvotes: 1
Views: 436
Reputation: 124
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();
?>
Upvotes: 2
Reputation: 187
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");
}
Upvotes: 0