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
Get the solution ↓↓↓I'm using anisset
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 useisset
it will returnTRUE
if the variable is set and the value is notNULL
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: constant expression contains invalid operations
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.