I have the following code which inserts multiple records into MySQL:
foreach ($_POST AS $input => $value) {
if (stristr($input, 'percent_owner_')) {
$memberID = str_replace('percent_owner_', '', $input);
$data['memberID'][] = $memberID;
$data['percentOwner'][] = $value;
}
}
$total = array_sum($data['percentOwner']);
if ($total !=100) {
$error = "Must total 100%.";
include('./errors/shares_error.php');
} else {
$c = count($data['memberID']);
for ($i=0;$i<$c;$i++) {
$asset_ID = $_GET['asset_ID'];
$query = "INSERT INTO shares (asset_ID, member_ID, percent_owner)
VALUES ('$asset_ID', '{$data['memberID'][$i]}', '{$data['percentOwner'][$i]}')";
$add_shares = $db->exec($query);
}
}
If the ‘percent_owner’ value is 0, I don’t want that record inserted. I attempted to put an if statement within the for, checking to see if '{$data['percentOwner'][$i]}' !== 0 but was not able to get it to work. Any help would be appreciated.
Thanks.
Compare the value
$data['percentOwner'][$i]against 0 after casting it as an integer. It appears to be a string from the way you have produced the value above: