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")
;
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
}
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
}
SELECT
*
FROM
`ident`
WHERE
FirstName = '$first_name'
&& Age != ''
&& Age = $age
&& Age IS NOT NULL;
if ($age !== null) {
$result = mysqli_query("select * from ident where FirstName = '$first_name' Age = $age");
//etc..
}
You should check this on PHP page before querying the database.
<?php
if(!empty($age)) {
$result = mysqli_query("select * from ident where FirstName = '$first_name' AND Age = '$age'");
}
?>
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");
}