当我仅更新1个数据时,我所有的数据都更改了。有人可以帮助检查我的代码吗?

时间:2019-02-15 07:59:43

标签: c#

 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();

有人尝试了几个小时,但仍然有相同的输出吗?

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

您在更新查询中缺少WHERE子句。

您应该有类似的内容:

WHERE ID='the id of the line you want to update'