In a script I receive a variable value with GET request and I have to explode it by a delimiter but sometimes part of the string is empty. How do I declare that in the INSERT query to insert NULL if string is empty?
$statement = $db->prepare('insert into _table (column1, column2, etc.) values (?, ?, etc.)
if (isset($_GET['var']) || isset($_GET['var1'])) {
$pieces=explode("delimeter", $var);
// $data = array($somevar, $someothervar, $pieces[0], pieces[1], etc.);
} else {
// other things to do with // $data = array($somevar, $someothervar, etc.);
}
$statement->execute($data);
Sometimes it happens that the value of $pieces[0], $pieces[1], etc is "" (quote excluded, just for reference), or so to say empty. How do I make it insert NULL in the database if any of the exploded pieces are empty? Not really sure where and how to declare the !empty check because of the isset. Current code enters blanks in the table rows instead of NULL for the empty exploded pieces. Please note that the exploded pieces are not always empty and sometimes not all of them are empty, sometimes all are empty. Depends on the case. :)