I have a form that users can fill in with profile information, all form fields are optional (not required).
My issue is i am trying to go through all form fields and any form fields that are empty assign the value NULL and for form fields that aren't empty assign what user put in form.
Rather than multiple if statements i thought a foreach would be easier and mean cleaner code, but it's the foreach and if statement within the foreach loop that's causing my issue which i do not know how to resolve.
I have not included all the code as don't think it's needed for my issue.
// fetch form data
$formdata = filter_input_array(INPUT_POST);
// assign session to variables
$user_uid = $_SESSION['user_uid'];
$user_login = $_SESSION['user_login'];
foreach ($formdata as $data){
if (empty($data)) {
$formdata = NULL;
}
}
$url = $formdata['url'];
$facebook = $formdata['facebook'];
$twitter = $formdata['twitter'];
$instagram = $formdata['instagram'];
$youtube = $formdata['youtube'];
// connect to database
$dbh = sql_con();
// process to db
$stmt = $dbh->prepare('
UPDATE
users_profiles
INNER JOIN
users
ON
users_profiles.user_uid = users.user_uid
SET
user_url = :user_url,
user_facebook = :user_facebook,
user_twitter = :user_twitter,
user_instagram = :user_instagram,
user_youtube = :user_youtube
WHERE
users_profiles.user_uid = :user_uid
AND
users.user_login = :user_login
');
// execute query
$stmt->execute(
array(
':user_url' => $url,
':user_facebook' => $facebook,
':user_twitter' => $twitter,
':user_instagram' => $instagram,
':user_youtube' => $youtube,
':user_uid' => $user_uid,
':user_login' => $user_login
));
If i submit form without filling any form fields, database entry for user gets updated with NULL as i want but if i fill in some or any form field and submit the information i provided in form, still shows NULL in database and does not update with what user entered in form.
i expect form fields that weren't filled in to be assigned value NULL and fields that were filled in to be assigned what user put in form then be updated in database.
Thank you for any help.