【问题标题】:How to stop value only returning true when selecting boolean column from database [closed]从数据库中选择布尔列时如何停止仅返回true的值[关闭]
【发布时间】:2021-06-27 23:27:05
【问题描述】:

我正在构建一个具有两种不同用户类型的登录系统。在用户字段中,它们是一个名为 user_isAdmin 的布尔列。如果为 true,则用户为管理员,如果为 false,则用户为默认用户。目前我已经编写了一个登录但当前为每个用户返回 true 的代码。这是第一次尝试:

    private void confirmBtn_Click(object sender, EventArgs e)
    {
        Connect database = new Connect();
        String username = usernameField.Text;
        String password = passwordField.Text;

        DataTable table = new DataTable();
        MySqlDataAdapter adapter = new MySqlDataAdapter();
        MySqlCommand command = new MySqlCommand("SELECT * From tbl_user WHERE `username` = @uname and `user_password` = @pwd", database.getConnection());
        command.Parameters.Add("uname", MySqlDbType.VarChar).Value = username;
        command.Parameters.Add("pwd", MySqlDbType.VarChar).Value = password;

        adapter.SelectCommand = command;
        adapter.Fill(table);


        if (!checkInputFields())
        {
            if (username.ToLower().Trim().Equals("") || password.Trim().Equals(""))
            {
                /*Please Enter Username or Password*/
                MessageBox.Show("Please Enter Username or Password!", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
            else if (table.Rows.Count > 0)
            {
                /*Login Success for Admin*/
                if (!checkIfAdmin()) {
                    MessageBox.Show("Admin Login Successful!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    HomeAdmin admin = new HomeAdmin();
                    admin.ShowDialog();
                }
                else
                {
                    /*Login Success for user*/
                    MessageBox.Show("User Login Successful!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    HomePlayer user = new HomePlayer();
                    user.ShowDialog();
                }
            }
            else
            {
                /*Login Error No Username*/
                if (username.Trim().Equals(""))
                {
                    MessageBox.Show("Please Enter Your Username", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                /*Login Error No password*/
                else if (password.Trim().Equals(""))
                {
                    MessageBox.Show("Please Enter Your Password", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                else
                {
                    /*Login Error Wrong Username or password*/
                        MessageBox.Show("Username or Password is incorrect", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
            }
        }
        else
        {
            MessageBox.Show("Please Enter all your details", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
        }
    }
    /*Check All Fields Have Inputs*/
    public Boolean checkInputFields()
    {
        String username = usernameField.Text;
        String password = passwordField.Text;
        if (username.ToLower().Trim().Equals("username") || password.ToLower().Trim().Equals("password"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public Boolean checkIfAdmin()
    {
        Connect database = new Connect();
        MySqlCommand command = new MySqlCommand("SELECT user_isAdmin From tbl_user WHERE userID = userID", database.getConnection());
        command.Connection.Open();
        bool isAdmin = (bool)command.ExecuteScalar();

        if (isAdmin)
        {
            return true;

        }
        else
        {
            return false;
        }
       
    }

它到达调用 checkIfAdmin 的部分:

if (!checkIfAdmin()) {
                    MessageBox.Show("Admin Login Successful!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    HomeAdmin admin = new HomeAdmin();
                    admin.ShowDialog();
                }
                else
                {
                    /*Login Success for user*/
                    MessageBox.Show("User Login Successful!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    HomePlayer user = new HomePlayer();
                    user.ShowDialog();
                }

这是 checkIfAdmin:

    public Boolean checkIfAdmin()
{
    Connect database = new Connect();
    MySqlCommand command = new MySqlCommand("SELECT user_isAdmin From tbl_user WHERE userID = userID", database.getConnection());
    command.Connection.Open();
    bool isAdmin = (bool)command.ExecuteScalar();

    if (isAdmin)
    {
        return true;
    }
    else
    {
        return false;
    }

}

目前它只返回 true 表示所有用户都是管理员,即使他们认为他们不是。 我想知道的是如何根据他们是否是管理员来选择 user_isAdmin 值。如果他们是管理员,调用 checkIsAdming 将运行:

                    MessageBox.Show("Admin Login Successful!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Hide();
                HomeAdmin admin = new HomeAdmin();
                admin.ShowDialog();

而用户会运行:

                    MessageBox.Show("User Login Successful!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Hide();
                HomePlayer user = new HomePlayer();
                user.ShowDialog();

如果有帮助,这是我的用户表:

【问题讨论】:

  • tbl_user 中的纯文本密码,未散列密码?真的吗?不要那样做。请。因为网络蠕虫。阅读密码哈希。
  • 目的不是为了一个真正的公共网站,它只是一个用于评估的本地游戏
  • 我不想让事情变得复杂,只是为我自己的用户提供一个简单的注册表单,这样我就可以创建多个用户
  • WHERE userID = userID 总是正确的
  • "SELECT user_isAdmin From tbl_user WHERE userID = userID" 没有为userID 使用变量。您不应该先从username 中查找id,然后将该id 作为参数传递给此select 语句吗?

标签: c# mysql visual-studio winforms


【解决方案1】:

正如其他人指出的那样,当您使用 SELECT user_isAdmin From tbl_user WHERE userID = userID 时,您并没有为特定的用户 ID 选择一行,因为您没有将用户 ID 作为参数传递。

您使用的命令等同于SELECT user_isAdmin From tbl_user WHERE 1 = 1。执行此命令时,它将返回表中所有用户的 user_isAdmin 值列表。

为了实现你想要的,你需要获取唯一用户的 user_isAdmin 值。这可以通过将 userID 作为 参数 传递给您的 SQL 查询来完成,就像您在此处所做的那样:SELECT * From tbl_user WHERE username = @uname and user_password = @pwd

CheckIfAdmin 方法应如下所示:

public Boolean CheckIfAdmin(Guid userId) // or whatever is the type of userId
{
    Connect database = new Connect();
    MySqlCommand command = new MySqlCommand("SELECT user_isAdmin From tbl_user WHERE userID = @userID", database.getConnection());
    command.Parameters.Add("userID", MySqlDbType.Guid).Value = userId;        
    command.Connection.Open();
    bool isAdmin = (bool)command.ExecuteScalar();

    if (isAdmin)
    {
        return true;
    }
    else
    {
        return false;
    }

}

这将获得相关用户的用户 ID。

当然,在调用 CheckIfAdmin 之前,您必须从数据库中获取用户 ID。 如果用户名是唯一的(我推荐),您可以使用它来代替 userID。

【讨论】:

  • 当您在我的注册表单中创建它们时,所有用户名都是唯一的。当table.Rows.Count > 0 为真时,它会调用checkIfAdmin。默认情况下,所有用户都设置为 false。我已经手动设置了一个管理员来测试。用户 ID 是 userID,user_isAdmin 是布尔值,用户名是 varchar。我可以根据用户名来做,但我相信选择userID 会更好。 @uodami
  • 这里需要传入参数吗if (!CheckIfAdmin("parameters")) {MessageBox.Show("Admin Login Successful!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Hide();HomeAdmin admin = new HomeAdmin();admin.ShowDialog();}
  • 是的,您应该调用CheckIfAdmin(userId)CheckIfAdmin(username),具体取决于您是通过ID 还是用户名获取用户。
  • 当我做CheckIfAdmin(userId)时,它会抛出the name userID does not exist in the current context
  • 感谢您的帮助。虽然这不是确切的解决方法,但它非常接近我正在寻找的东西
【解决方案2】:

我找到了一种可以完成这项工作的方法: 我将一个名为username 的参数传递给CheckIfAdmin

 if (!CheckIfAdmin(username)) {
                    MessageBox.Show("Admin Login Successful!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    HomeAdmin admin = new HomeAdmin();
                    admin.ShowDialog();
                }
                else
                {
                    /*Login Success for user*/
                    MessageBox.Show("User Login Successful!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    HomePlayer user = new HomePlayer();
                    user.ShowDialog();
                }

我将CheckIfAdmin 更改为:

        public Boolean CheckIfAdmin(String username) // or whatever is the type of userId
    {
        Connect database = new Connect();
        MySqlCommand command = new MySqlCommand("SELECT user_isAdmin From tbl_user WHERE username = @username", database.getConnection());
        command.Parameters.Add("username", MySqlDbType.VarChar).Value = username;
        command.Connection.Open();
        bool isAdmin = (bool)command.ExecuteScalar();

        if (isAdmin)
        {
            return false;
        }
        else
        {
            return true;
        }

    }

这样的结果似乎是我想要的,所以我认为它是正确的。我交换了真假。如果用户登录,则会弹出用户框,如果管理员登录,则会弹出管理员框。我不确定这是否是一个好方法,但它确实有效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2014-06-10
    相关资源
    最近更新 更多