Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Insert only populated fields into MySQL

im trying to insert only populated ID fields to MySQL, meaning if two out of three are populated, only two to be inserted. I got blind with many code lines, and can’t see where Im making a mistake.

Logic is that I’ll search for the serial number, once selected it will populate row ID from the serial number into the ID field.
I want to pass ID field value to the database. However, below code is submitting all ID fields even the empty ones (where serial number is not selected), and I dont need to pass records for not populated ID field.
Where am I making a mistake?

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$idCount = count($_POST['assetsn_id']);
echo $idCount;
for($i=0; $i < $idCount; ++$i) {
    $assetsn_id = $_POST['assetsn_id'][$i];
    $assetsn_location_address = $_POST['assetsn_location_address'];
    $assetsn_location_rack = $_POST['assetsn_location_rack'];
    $assetsn_location_shelf = $_POST['assetsn_location_shelf'];
    $assetsn_location_bin = $_POST['assetsn_location_bin'];
    $assetsn_location_createdby = $_SESSION['user']['id'];
    $assetsn_location_userlogid = $_SESSION['user']['userlogid'];

$sql = "INSERT INTO asset_serial_locations SET assetsn_id=?, assetsn_location_address=?, assetsn_location_rack=?, assetsn_location_shelf=?, assetsn_location_bin=?, assetsn_location_createdby=?, assetsn_location_userlogid=?";
$result = modifyRecord($sql, 'sssssss', [$assetsn_id, $assetsn_location_address, $assetsn_location_rack, $assetsn_location_shelf, $assetsn_location_bin, $assetsn_location_createdby, $assetsn_location_userlogid]);
    
if ($result) {
    $_SESSION['success_msg'] = "Location updated successfully!";
    header("location: " . BASE_URL . "workshop/location/");
    exit(0);
} else {
    $_SESSION['error_msg'] = "Something went wrong. Could not update locations.".'<br />'.mysqli_error($conn);
}
}
Serial number #1
<input class="form-control form-control-lg" type="search" name="" autocomplete="off" placeholder="search for serial number..." />
<input class="form-control form-control-lg" type="text" name="assetsn_id[]" autocomplete="off" placeholder="ID" /><br /><br />
Serial number #2
<input class="form-control form-control-lg" type="search" name="" autocomplete="off" placeholder="search for serial number..." />
<input class="form-control form-control-lg" type="text" name="assetsn_id[]" autocomplete="off" placeholder="ID" /><br /><br />
Serial number #3
<input class="form-control form-control-lg" type="search" name="" autocomplete="off" placeholder="search for serial number..." />
<input class="form-control form-control-lg" type="text" name="assetsn_id[]" autocomplete="off" placeholder="ID" />

>Solution :

Use array_filter to remove nulls values from an array :

$_POST['assetsn_id'] = array_filter($_POST['assetsn_id']);
$idCount = count($_POST['assetsn_id']);
echo $idCount;
for($i=0; $i < $idCount; ++$i) {
....
}

Here what array_filter can remove :

$a = array(0, '0', NULL, FALSE, '', array());
var_dump(array_filter($a));
// array()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading