I wonder if there are better ideas for solving the problem below,
I have a form with a number of input fields, for example,
<input name="pg_title" type="text" value="" /> <input name="pg_subtitle" type="text" value="" /> <input name="pg_description" type="text" value="" /> <input name="pg_backdate" type="text" value="" /> etc
But sometimes I donβt need certain input fields above in my form, for example, I only need the page title for my db injection,
<input name="pg_title" type="text" value="" /> etc
And I have another php page for processing $_POST data,
$pg_title = null; $pg_subtitle = null; $pg_description = null; $pg_backdate = null; if(isset($_POST['pg_title']) && !empty($_POST['pg_title']) ) $pg_title = $_POST['pg_title']; if(isset($_POST['pg_subtitle']) && !empty($_POST['pg_subtitle']) ) $pg_subtitle = $_POST['pg_subtitle']; if(isset($_POST['pg_description']) && !empty($_POST['pg_description']) ) $pg_description = $_POST['pg_description']; if(isset($_POST['pg_backdate']) && !empty($_POST['pg_backdate']) ) $pg_backdate = $_POST['pg_backdate'];
Every time I need to check if $_POST set to a specific input field and not empty , otherwise its variable will be set to null , so I will not enter empty space in my db.
I find that isset and !empty in the if condition are very repeated when I have a long list of variables to be processed.
Is there a default php function for the < cut "process above? Or do I need to write a user definition function to handle this?
Or maybe there is another way to do this?
Thanks.
EDIT:
Thank you very much for your help.
Just some kind of extra code on my php page that processes $ _POST data,
$sql = " UPDATE root_pages SET pg_url = ?, pg_title = ?, pg_subtitle = ?, pg_backdate = ?, pg_description = ?, ... updated_by = ? WHERE pg_id = ? "; $result = $connection->run_query($sql,array( $pg_url, $pg_title, $pg_subtitle, $pg_backdate, $pg_description, ... $pg_id ));
since you see that $pg_subtitle, $pg_backdate, $pg_description, etc always present in my request. so if i get $pg_subtitle = '' instead of $pg_subtitle = null when there is no data in it, my db record will have an empty space for this column.
Thanks: -)