'How to skip blank values of array when updating columns in the database
I'm a newbie in PHP. I would like to ask on how to skip blank values in an array when updating columns in the database. My code goes this:
$reference = $_POST['ref'];
$userDoc = $_POST['userDoc'];
$doc = array_filter($userDoc);
$ref = array_filter($reference);
for($j=0; $j<=sizeof($doc_ent);$j++){
$docu = $doc_ent[$j];
echo $ref[$j];
echo $doc[$j];
$update = sqlsrv_query($conn2, "update stock_picking_se set name = '$internal$ref[$j]', user_doc = '$doc[$j]' where doc_entry = '$docu'");
}
My checkbox which filter my data is:
checkbox1: a
checkbox2: (blank data)
checbox3: b
checkbox4: (blank data)
checkbox5: c
But when I process, it updates the existing columns but keep inserting the blank value in an array, the result is:
column:
name user_doc
1 a
(blank) (blank)
2 b
note that $doc_ent is the the selected checkboxes. I want the result to insert in the column name 1,2,3 and in column user_doc a,b,c.
Solution 1:[1]
use empty();
empty — Determine whether a variable is empty
if(!empty($reference) && !empty($userDoc)) { Put your query } .
Solution 2:[2]
You have to put a condition:
if($reference != '' && $userDoc != '') { Put your query }
Then it automatically skips the blank fields.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | underscore |
| Solution 2 | vinzee |
