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.
Have a default value if $work->end_date does not exist?
Something like this:
$end_date = empty($work->end_date)? 0 : $work->end_date
$stmt->bindParam(':end_date', $work->end_date, PDO::PARAM_STR);
You could always try to write a wrapper to bindParam
which 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.