If the user sends AGE value null then don't execute. How do I write properly in MySQL
$result = mysqli_query("select * from ident where FirstName = '$first_name' && $age != '' && Age = $age");
You can use
if(!empty($age)){
//do sql operation
}
You can also add constraints if you want only specific age groups. example:if you want age group between 18 and 40
if ($_POST["age"] < 18 || $_POST["age"] > 40){
//print error message
}
else{
//do sql operation
}
if ($age !== null) {
$result = mysqli_query("select * from ident where FirstName = '$first_name' Age = $age");
//etc..
}
You weren't very clear in your question, so I'll provide for both PHP & mySQL:
mySQL
Use the IS NOT NULL. Reference
SELECT * FROM ident
WHERE FirstName = '$first_name'
AND Age IS NOT NULL
AND Age = $age
PHP
Use empty() or isset(). Reference.
if(!empty($age)){
//execute mySQL
}
This should be done on query building side, you might want to throw an exception if certain values are not met as expected
PHP
Use is_null() method or as said by @Huey
if(isset($age) and !is_null($age) and intval($age) > 0){
$result = mysqli_query("SELECT * FROM ident WHERE FirstName = '$first_name' AND Age = $age");
}
$agewill be null?