foreach循环,检查批准,显示错误数组

时间:2013-07-02 21:14:27

标签: php mysql sql

我有一个带复选框的表格和一个允许用户“被批准”的按钮,数据库中的简单1标记它们已被批准。我想首先检查用户是否已获得批准,以便我可以显示错误消息。但是,在此代码中最终发生的是由于某种原因代码执行底层sql,但按原样显示错误消息,或者!$check始终显示成功消息。我尝试了不同的变化,但我感觉问题是我的$ check sql逻辑。我似乎无法看到它。

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');
}

1 个答案:

答案 0 :(得分:1)

   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');
}