php - Using isset() to check for empty fields and if the field is empty it should jump to the else statement but the statement is not working ← (PHP)
I'm using an isset
to check for empty fields and if the field is empty it should jump to the else statement but the statement is not working.
<pre>
if(isset($_POST['make'])){
if(is_numeric($_POST['mileage']) && is_numeric($_POST['year'])){
$stmt = $pdo->prepare('INSERT INTO autos (make, year, mileage) VALUES ( :mk, :yr, :mi)');
$stmt->execute([
':mk' => $_POST['make'],
':yr' => $_POST['year'],
':mi' => $_POST['mileage'])
];
$_SESSION['success']="Added successfully";
header("Location: view.php");
return;
}else{
$_SESSION['error']="Mileage or Year must be numeric.";
header("location:add.php");
return;
}
}else{
$_SESSION['error']="Make is required.";
header("location:add.php");
return;
}
</pre>
Any kind of help would be appreciated.
Answer
Solution:
I would recommend you to use instead of
to check the emptyness.
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.
And if you use isset
it will return TRUE
if the variable is set and the value is not NULL
Determine if a variable is considered set, this means if a variable is declared and is different than NULL. isset() will return FALSE when checking a variable that has been assigned to NULL. Also note that a null character ("\0") is not equivalent to the PHP NULL constant.