Logic_hook in loop

I will briefly explain my problem:
when I save a custom form, a file is executed (via logic_hook before), in addition to various operations, in this file I need to update some fields common to all the records in this form, so I loop through all the records, update the value and save inside the foreach.
The problem is just the foreach, every time I save the record the logic_hook I am in is called, generating a loop… Eventually everything works, but saving takes a huge amount of time.
Can anyone tell me if there is a way to update the records without generating a loop? I need an idea, thanks.

Use direct SQL with an UPDATE statement; this will be infinitely faster, and will avoid any hook re-entries.

A bit risky to update the DB directly, but if there is no alternative… I only need to update 2 fields per record. I will test it. Thanks pgr.

Excuse me pgr, do you think it is correct to put the update in a loop? I wouldn’t want the DB to have problems over time:

$sql1="UPDATE <MODULE_CSTM> SET <FIELD>=".$field." WHERE id_c='".$id."'";
$db->query($sql1);

I don’t understand your question about putting the UPDATE in a loop.

I recommend doing a single SQL UPDATE (which can update all the records in that table in one go, if that’s your requirement).

Don’t create a loop, with an UPDATE for a single record in each iteration; this going in and out of the SQL engine into the PHP world will take a lot of time. If you need to update all records in the table, just do one UPDATE without a WHERE condition.