简体   繁体   中英

foreach loop, check for approval, display error array

I have a table with checkboxes and a button that allows users to be "approved", a simple 1 in the database marks they have been approved. I want to first check to see if the user is ALREADY approved, just so that I can display an error message. However what ends up happening in this code is that for some reason the code executes the underlying sql, but displays the error message as is, or when !$check always display a success message. I have tried different variations but I have a feeling the problem is with my $check sql logic. I just can't seem to see it.

if ($_POST['doApprove'] == 'Approve') {
    if (!empty($_POST['u'])) {
        foreach ($_POST['u'] as $uid) {
            $id = filter($uid);
            $username[] = get_user_name_from_id($id);
            $check = Nemesis::select("approved", "users", "id = '{$id}' AND approved <> '1'");  // issue?
            $set_approve = Nemesis::update("users", "approved = '1'", "id = '{$id}'");
            if ($check) { // issue?
                $fail[] = $username;
            } elseif (!$set_approve) {
                $fail[] = $username;
            } else {
                $pass[] = $username;
                $user_details = Nemesis::select("*", "users", "id = '{$id}'");
                $ud_row = $user_details->fetch_assoc();
                authMail('register', $ud_row['user_email'], $ud_row['pwd'], $ud_row['user_name'], NULL, 'admin');
            }
        }
    }
    $msg = new Messages();
    if (!empty($pass)) {
        $passed = implode(', ', $pass);
        $message = "Approved: {$passed}";
        $msg->add('s', $message);
    }
    if(!empty($fail)){
        $failure = implode(', ', $fail);
        $message = "Could not approve {$failure}";
        $msg->add('e', $message);
    }
    redirect('users.php');
}
   if ($_POST['doApprove'] == 'Approve') {
    if (!empty($_POST['u'])) {
        foreach ($_POST['u'] as $uid) {
            $id = filter($uid);
            $username[] = get_user_name_from_id($id);
            $check = Nemesis::select("*", "users", "id = '{$id}'");
            $ud_row = $user_details->fetch_assoc();
            $set_approve = Nemesis::update("users", "approved = '1'", "id = '{$id}'");
            if ($ud_row['approved'] || !$set_approve) {
                $fail[] = $username;
            } else {
                $pass[] = $username;
                authMail('register', $ud_row['user_email'], $ud_row['pwd'], $ud_row['user_name'], NULL, 'admin');
            }
        }
    }
    $msg = new Messages();
    if (!empty($pass)) {
        $passed = implode(', ', $pass);
        $message = "Approved: {$passed}";
        $msg->add('s', $message);
    }
    if(!empty($fail)){
        $failure = implode(', ', $fail);
        $message = "Could not approve {$failure}";
        $msg->add('e', $message);
    }
    redirect('users.php');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM