Skip to content
Advertisement

Stopping rand() if I dont have any data on dropdown list PHP

So I have a basic PHP site with a drop down list what’s having it’s datas from a MySql database. I made a random number generator after a dropdown list what is working if you’ve selected both options. I tried with an if else but it’s always showing even if it’s didn’t have any data from the list.

if (isset($_GET['kavenev']) || (isset($_GET['sutinev'])) && !empty($_GET['kavenev']) || !empty($_GET['sutinev'])){
            $kavenev=$_GET['kavenev'];
            $sutinev=$_GET['sutinev'];
            echo "<h2>Kuponkód: </h2>";
            echo (rand(1,100000000) . "<br>");
        } else {
            echo "Error!";
        }

The code stands after the drop down list’s item selecting. I’m a rookie so thanks for the answers!

Advertisement

Answer

You have to write your if-statemante so:

if (!empty($_GET['kavenev']) && !empty($_GET['sutinev'])) {
 [...]
} else {
 echo 'Error!';
}

isset => check if the variable is set (null | true | false | string | what ever)

empty => check if the variable is empty … an empty state is: (null, false, “”, “not isset()”).

so empty checks the same way as isset and on top “null”, “false”, “”.

Take a look at the documentation: https://www.php.net/manual/en/function.empty.php

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement