Foreach Loop to Insert into table
Posted: 2022-05-10 07:43
Hello,
I have a little problem
I have a Persons table and a Letters table. With the help and addition of the Mass Update plugin, after selecting people and selecting a letter (dropdown via More option), the addressed letters are displayed directly in TCPDF - this works wonderfully
In the second function of the MassUpdate plugin, an ajax file is now called up, to which the selected person IDs (array IDS) and the ID of the letter (new_value) are transferred.
The person IDs should now be entered as new data records in the table testtable using a foreach loop, see the following code (customized code from the mass-update-plugin):
This works so far, each individual person ID is entered as a new data record
Now the letter ID (new_value) should also be transferred to field3 in every record.
Than I change the Query command to:
Now only one person ID is entered in field2 and the letter ID in field3, but not for all person IDs
Can someone give me a tip here?
kind regards
Stefan
I have a little problem

I have a Persons table and a Letters table. With the help and addition of the Mass Update plugin, after selecting people and selecting a letter (dropdown via More option), the addressed letters are displayed directly in TCPDF - this works wonderfully

In the second function of the MassUpdate plugin, an ajax file is now called up, to which the selected person IDs (array IDS) and the ID of the letter (new_value) are transferred.
The person IDs should now be entered as new data records in the table testtable using a foreach loop, see the following code (customized code from the mass-update-plugin):
Code: Select all
/* receive and validate calling parameters */
$ids = Request::val('ids');
if(empty($ids) || !is_array($ids)) {
@header($_SERVER['SERVER_PROTOCOL'] . ' 501 Not Implemented');
exit;
}
$new_value = makeSafe(Request::val('newValue'));
foreach($ids as $id) {
$tn2 = 'testtable';
$field2 = 'field2';
$field3 = 'field3';
$query = "INSERT INTO `{$tn2}` (`{$field2}`) VALUES ('{$id}')";
$e = ['silentErrors' => true];
sql($query, $e);
}
if($e['error']) {
@header($_SERVER['SERVER_PROTOCOL'] . ' 501 Not Implemented');
}

Now the letter ID (new_value) should also be transferred to field3 in every record.
Than I change the Query command to:
Code: Select all
$query = "INSERT INTO `{$tn2}` (`{$field2}`, `{$field3}`) VALUES ('{$id}', '{$new_value}')";

Can someone give me a tip here?
kind regards
Stefan