Here's what I'm doing.
- I'm checking if there's a "version" value in the URL with $get_version.
- Get the latest version from the database and set as a default variable.
- If the URL variable is good, check the database to see if it exists then set the appropriate variables.
- If doesn't exist, use default value from above.
It always goes to the "Bad query section". Either my query is wrong or my if statement doesn't work.
Here's my code. Also, is there a cleaner way of doing it?
// Check if there's a version in URL. If not, set to empty.
$get_version = isset($_GET['version']) ? $_GET['version'] : '';
// Set defaults if nothing in URL
$query = "SELECT * FROM sn_hockey_versions ORDER BY version_id DESC LIMIT 1";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
while ($row = mysqli_fetch_array($result)) {
$newest_version_id = $row['version_id'];
$newest_sections = $row['sections'];
}
if (!empty($get_version) && preg_match('/^[0-9.]*$/', $get_version)) {
$query = "SELECT version_id, sections FROM sn_hockey_versions WHERE version = '".$get_version."'";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
if ($row = mysqli_fetch_array($result)) {
$set_version = $row['version_id'];
$v_sections = $row['sections'];
$test = "IT WORKS!!!!";
}
else {
$set_version = $newest_version_id;
$v_sections = $newest_sections;
$test = "Bad query";
}
}
else {
$set_version = $newest_version_id;
$v_sections = $newest_sections;
$test = "Set default";
}