Hi guys,
I have a problem with inserting multiple values from 1 user, to other users in a loop.
Problem is that that i get inserted just 1 row for each user, but i need to insert multiple rows.
As you can see i use foreach to get multiple results from 1 user so i can insert for every other.
For example:
user with client_id 67 holds multiple values in a table that i need,
users with id’s 99, 111, 113, 130 need to have same data as id 67 have
If i use var_dump($pr2) i get values same as on image bellow code
foreach ($pr2 as $r) = holds values i need but won’t insert them all
Here is a code
$user_role = intval($_POST['user_role']);
$userID = intval($_POST['user_id']);
$groupID = intval($_POST['group_id']);
$ea_settings = $_POST['ea_settings'];
// get users from group to apply ea_settings
$get_users = "SELECT a.group_name, b.user_id, b.user_role, b.group_id, c.email, c.ea_settings
FROM user_groups a
INNER JOIN users_groups b ON b.group_id = a.id
INNER JOIN www_users c ON c.id = b.user_id
WHERE a.id = '".$groupID."'";
$pr = fetch_array($get_users);
// get special user relations
$query2 = "SELECT a.*, b.user_role
FROM signal_providers_relations a
INNER JOIN users_groups b ON a.client_id = b.user_id
WHERE a.client_id = '".$userID."' AND b.user_role = '1'";
$pr2 = fetch_array($query2);
// update all users where id = all id's from group, apply ea_settings to all id's from Special user
foreach ($pr as $key) {
foreach ($pr2 as $r) {
$user_id = intval($key['user_id']); // id of all users
// update users
$update_users = "UPDATE www_users SET ea_settings = '".$ea_settings."' WHERE id = '".$user_id."'";
if (exec_query_confirm($update_users)) {
// first delete all user record in relations that are not special users
if ($key['user_role'] == '0') {
$delete_relations = "DELETE FROM signal_providers_relations WHERE client_id = '".$user_id."'";
exec_query($delete_relations);
// if deleting is successfully get all relations from special user and insert to all other users
$insert_new_values = "INSERT INTO signal_providers_relations (client_id, provider_id, active_sp, subscription_sp, created_by, cr_datetime, ea_settings_active_sp, ea_settings_sp, up_datetime, sp_risk_allocation, sp_max_trades)
VALUES ('".$user_id."', '".$r['provider_id']."', '".$r['active_sp']."', '".$r['subscription_sp']."', '".$r['created_by']."', '".$r['cr_datetime']."', '".$r['ea_settings_active_sp']."', '".$r['ea_settings_sp']."', '".$r['up_datetime']."', '".$r['sp_risk_allocation']."', '".$r['sp_max_trades']."')";
if (exec_query_confirm($insert_new_values)) {
echo 'Executed';
} else {
echo 'Not executed';
}
}
} else {
$err = 61; // not Updated
}
} // foreach
} // foreach
}
Here is a image from 1 st user and data i want to insert to others
https://prnt.sc/jft943