Array named date_day consists (includes) 2 elements. For example the first is 11 and the second is blank (’ ').
Need to insert in mysql only element that is not blank. In such case one row with value - 11 and the blank value does not insert at all.
Tried to use strlen.
With this code
foreach ($_POST['date_day'] as $i => $date_day) {
$strlen = (strlen($date_day));
echo $strlen .' strlen<br>';
}
I get echo
2 strlen
0 strlen
So for the first element (11) strlen is 2 and for the second blank strlen is 0. That is ok.
Next mean insert array each element which string length is more than 0
Used this code
if ( (strlen($date_day)) > 0 ) {
$sql .= implode(', ', $insertQuery) . 'ON DUPLICATE KEY UPDATE RecordDay = VALUES(RecordDay);';
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
But behavior is not as expected. If both elements blank, then no insert (that is ok). If both elements not blank, then insert (that also ok). But not ok is: if at least one element is not blank, then insert 2 rows, one with value, and other blank row.
What is wrong with the code? I mean with
foreach ($_POST['date_day'] as $i => $date_day)
$date_day
is each element of the array $_POST[‘date_day’]. Then with
if ( (strlen($date_day)) > 0 ) {
if particular $date_day strlen is more than > 0, insert; if not, does not insert.
Do some if’s with [FPHP]empty[/FPHP]
or
Just [FPHP]array_filter[/FPHP] without a callback, which will get rid of the empty values (also 0’s)
You might also want to sanitize the input (cast everything as an int)
Thank you for reply
Possibly I do not understand
Changed code to this
foreach ($_POST['date_day'] as $i => $date_day) {
if (array_filter($_POST['date_day'])) {
$sql .= implode(', ', $insertQuery) . 'ON DUPLICATE KEY UPDATE RecordDay = VALUES(RecordDay);';
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
}
And however inserts empty row in mysql…
What is wrong?
array_filter takes place outside the loop, and not inside an if.
Filter your array first. That will remove any empty values.
Then you can loop through the array, knowing that any record left has a value.
Seems again I am missing something.
try {
$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($_POST['row_id'] as $i => $row_id) {
$insertQuery[] = '(?, ?)';
$insertData[] = $row_id;
$insertData[] = $_POST['date_day'][$i];
}
$filtered_array = (array_filter($_POST['date_day']));
foreach ($filtered_array as $i => $date_day) {
$sql .= implode(', ', $insertQuery) . 'ON DUPLICATE KEY UPDATE RecordDay = VALUES(RecordDay);';
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
}
Now thinking that
$filtered_array = (array_filter($_POST['date_day']));
foreach ($filtered_array as $i => $date_day) {
may be not necessary.
And would need to use array_filter for $insertQuery[] = ‘(?, ?)’;. But with $insertQuery[] = array_filter(‘(?, ?)’); does not insert and it is inside foreach.
… some mess in my head… trying to understand for which code need to use array_filter. As far as I understand for either $insertQuery[] or $insertData[]…
Changed again code and result the same (inserts empty row)
$filtered_array = (array_filter($_POST['date_day']));
try {
$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($_POST['row_id'] as $i => $row_id) {
$insertQuery[] = '(?, ?)';
$insertData[] = $row_id;
$insertData[] = $filtered_array[$i];
}
$sql .= implode(', ', $insertQuery) . 'ON DUPLICATE KEY UPDATE RecordDay = VALUES(RecordDay);';
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
Seems finally there is solution if need to check only one array (if not empty)
$filtered_array = (array_filter($_POST['date_day']));//change $_POST['date_day'] to array that includes only non-empty values
//print_r ($filtered_array);//check if array includes only non-empty values
//echo '$filtered_array<br>';
foreach ($filtered_array as $i => $filtered_array_value) {//here for inserting/updating use only thouse row_id where $_POST['date_day'] is not empty
$row_id_array[] = $_POST['row_id'][$i];
//print_r ($row_id_array);
//echo '$row_id_array<br>';
}
try {
$sql = "INSERT INTO 2_1_journal (Number, RecordDay) VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($row_id_array as $i => $row_id) {
$insertQuery[] = '(?, ?)';
$insertData[] = $row_id;
$insertData[] = $filtered_array[$i];
echo ($filtered_array[$i]) .' filtered_array[$i]<br>';
}
$sql .= implode(', ', $insertQuery) . 'ON DUPLICATE KEY UPDATE RecordDay = VALUES(RecordDay);';//as understand key is definied when created mysql table
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
Now question is: what if instead of $_POST[‘date_day’] also other $_POST need to check if not empty ($_POST[‘date_month’], $_POST[‘date_year’], $_POST[‘amount’])?
Can I use instead of foreach ($filtered_array as $i => $filtered_array_value) {
something like
foreach (($filtered_array as $i => $filtered_array_value) and ($filtered_array_month as $i => $filtered_array_value_month) and ($filtered_array_year as $i => $filtered_array_value_year) ) {
Is suppose it is not correct syntax
It sounds to me like you probably havent normalized your database/form if you’re doing all that looping. Perhaps a step back and look at the thing from a new angle is in order.
Actually trying to code accounting program. And this code is with following aim. I have multiple rows, where user can enter, date, amount, transaction etc. User start to enter. One row is completed, but for other row user may have not entered all required information. For example in amount field may not be number. In such case OK rows record in journal, but not OK rows save as draft (latter to review). That is why such code…
foreach($_POST['somefield'] AS $key => $value) {
if(!empty($_POST['somefield'][$key]) && !empty($_POST['quack'][$key]) && !empty($_POST['more'][$key]) && !empty($_POST['another'][$key])) {
$toInsert[] = array('somefield' => $_POST['somefield'][$key], 'quack' => (int) $_POST['quack'][$key], 'more' => $_POST['more'][$key], 'another' => $_POST['another'][$key]);
}
}
Foreach posted value, check to make sure all corresponding values exist. If so, add it to a new array to be used for insertion.
I threw in some sanitization in that ‘quack’ will be evaluated as an integer. Dont use unsanitized post values, so do whatever you need to to verify the integrity of the values.
Thank you! Now seems all is understandable for me