I have a form with many optional fields. I am using PHP and PDO to collect the data from the forms and store it in a DB.
Since some optional fields may be empty, I only want to store inputs that actually have value. I can easily identify the inputs that have value, but without using a whole lot of IF statements, I can't seem to find a way to ONLY insert a value into the DB if it exists.
My current PDO looks something like this:
$data = array($var1, $var2, $var3, $var4, $var5, $var6);
$STH = $DBH->("INSERT INTO users (first, last, addr, city, email, phone) values (?, ?, ?, ?, ?, ?)";
$STH->execute($data);
Is there a way to only update a column if a value in the array exists? Example: if $var3 and $var4 are empty, nothing is stored in the DB for "addr" and "city"?