A simplified example of a much more complex issue…
A variable is sometimes defined and sometimes isn’t. WITHOUT checking if the variable is empty, is there a way to execute the insert statement without the Query breaking?
$stmt = $this->db->prepare("INSERT INTO employment (user_id, start_date, end_date) VALUES (:user_id, :start_date, :end_date) ");
$stmt->bindParam(':user_id', $user->id, PDO::PARAM_INT);
$stmt->bindParam(':start_date', $work->start_date, PDO::PARAM_STR);
$stmt->bindParam(':end_date', $work->end_date, PDO::PARAM_STR);
$stmt->execute();
Sometimes $work->end_date may be non-existent.
Why “WITHOUT checking if variable is empty”? The primary foundation of the site changed and there would be a ton of variable checking. Yes, I know that shouldn’t be but it is the problem I inherited.
You could always try to write a wrapper to
bindParamwhich would check your if you variable is defined. If it is then it goes ahead and bindParam otherwise it skips it.Call it like
bindSafeParam(&$stmt, ':start_date', $work->start_date, PDO::PARAM_STR);