Insert NULL instead of empty string with PDO
If you want null, just insert null
instead of empty string.
$stmt2->bindParam(':title', $title === '' ? null : $title, PDO::PARAM_STR);
Besides xdazz's more appropriate answer, you can often solve something like this in SQL: Just rephrase the query INSERT INTO ... VALUES (?)
to something like INSERT INTO ... VALUES (NULLIF(?, ''))
.