首页 > 解决方案 > 当我只更新 1 个数据时,我的所有数据都发生了变化。有人可以帮忙检查我的代码吗?

问题描述

 protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int userid = Convert.ToInt32(GridView.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)GridView.Rows[e.RowIndex];
        Label lblID = (Label)row.FindControl("lblID");
        TextBox id = (TextBox)row.Cells[0].Controls[0];
        TextBox Username = (TextBox)row.Cells[1].Controls[0];
        TextBox Password = (TextBox)row.Cells[2].Controls[0];
        TextBox Email = (TextBox)row.Cells[3].Controls[0];
        GridView.EditIndex = -1;
        conn.Open();

        MySqlCommand cmd = new MySqlCommand("update user.register set Username='" + Username.Text  + "',Email='" + Email.Text + "',Password='" + Password.Text + "',id='"+id.Text+"'",conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        Gvbind();

任何人都可以帮我解决这个问题我已经尝试了几个小时但仍然是相同的输出

标签: c#

解决方案


好吧,您的查询缺少一个WHERE子句,它可能看起来像WHERE id = your_id. 如果您错过了该子句,正如您刚刚发现的那样,一条UPDATE语句将更新整个表。

另外,请阅读SQL injection,因为您的代码容易受到这种攻击。


推荐阅读