Hello,
Recently I am starting to code my scripts in PDO method to manage the information in a mysql database.
In this case I am having an issue how to use the best practice conditions when dealing with a POST int value.
With the posted values I am making a select query, a short version of it is as follows
if( isset($_POST['gjendja_id']) ){
$gjendja_id = intval($_POST['gjendja_id']);
if( $gjendja_id > 0 ){
$gjendja_id = $_POST['gjendja_id'];
}else{
$gjendja_id = 'IS NOT NULL';
}
}else{
$gjendja_id = 'IS NOT NULL';
}
The query that would be executed
try {
$counter = 1;
$response = '';
$stmt = $dbconnection->prepare('SELECT a.dokumenti, a.datafillimit, a.datambarimit, b.programi, c.lloji_diplomes, d.ial, e.akreditimi FROM programet_akreditimet AS a INNER JOIN programet AS b ON a.programi_id = b.id INNER JOIN programet_llojet_diplomave AS c ON b.lloji_diplomes_id = c.id INNER JOIN institucionet AS d ON b.ial_id = d.id INNER JOIN akreditimet_llojet AS e ON a.lloji_akreditimit_id = e.id WHERE a.datambarimit >= CURDATE() AND b.gjendja_id =:gjendja_id AND d.gjendja_id = 1 AND a.trashed = 0 ORDER BY d.ial ASC;');
$stmt->bindParam(':gjendja_id', $gjendja_id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row){
$response .='';
$counter++;
}
$response = array('error_code' => '0', 'response' => $response);
}catch(PDOException $exception){
$response = array('error_code' => '1', 'response' => 'Gabim gjatë kërkimit në databazë.');
//$exception->getMessage();
}
In this case I would have two scenarios
1- When the value of gjendja_id > 0 the condition would be b.gjendja_id =:gjendja_id(1, 2, 3, 4, ....)
2- When the value of gjendja_id < 1 the condition would be b.gjendja_id =:gjendja_id(IS NOT NULL)
Is any good example how can I deal with such cases?!
Thank you!