c# - 当我只更新 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();
任何人都可以帮我解决这个问题我已经尝试了几个小时但仍然是相同的输出
解决方案
好吧,您的查询缺少一个WHERE
子句,它可能看起来像WHERE id = your_id
. 如果您错过了该子句,正如您刚刚发现的那样,一条UPDATE
语句将更新整个表。
另外,请阅读SQL injection,因为您的代码容易受到这种攻击。
推荐阅读
- android - 在 Android Pie (API-28) 中进入活动时键盘不显示
- dotnetnuke - DNN - 500 内部服务器错误
- visual-studio-code - 使用外部 http 服务器在 vscode 中构建代码
- delphi - 从 BDE 迁移到 ADO - 数据库
- jenkins-pipeline - Jenkins 脚本化管道 - 顺序步骤
- asp.net - 如何使 Web 浏览器的 url 保持在 MVC 错误 404 中
- vba - 使用添加的变量值重命名文件夹中的所有文件而不打开
- spring - 来自 yaml 的 Spring 配置 - 结合 @ConfigurationEnabled 加载参数化文件名
- android - 虚幻引擎如何处理手机不同的屏幕分辨率?特别是用户界面
- javascript - 向有页脚的表格添加滚动条