我目前正在通过C#创建一个虚构的商店来练习ASP。
问题出在篮子里,因为每当点击第一行时(对于行删除),整个选择查询再次执行并附加到现有结果。
这对数据库没有影响,单击的其他行也能正常运行。
行(productid)的值隐藏在表单中并通过GET提交,并在另一个页面上检索,该页面删除与提交的ID相关联的购物篮数据。
购物篮页面
// #### SELECT ALL ITEMS THAT HAVE BEEN ORDERED BY THE USER #####
queryStr = @"SELECT products.ProductImage, products.ProductName,products.ProductDesc,products.ProductPrice,purchase.ProductID, purchase.Quantity
FROM products, purchase
WHERE purchase.UserID=?uid AND purchase.ProductID = products.ProductID";
cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn);
cmd.Parameters.AddWithValue("?uid", uid);
reader = cmd.ExecuteReader();
// pname = "";
while (reader.HasRows && reader.Read())
{
prodid = reader.GetInt32(reader.GetOrdinal("ProductID"));
pimg = reader.GetString(reader.GetOrdinal("ProductImage"));
pname = reader.GetString(reader.GetOrdinal("ProductName"));
desc = reader.GetString(reader.GetOrdinal("ProductDesc"));
price = reader.GetFloat(reader.GetOrdinal("ProductPrice"));
if (Session["uname"] != null)
{
output.InnerHtml += "<tr runat='server'><td><img src='" + pimg + "'></td><td>" + pname + "</td><td>" + desc + "</td><td>£" + price +
"</td><td> <form runat='server' action='removebasketitem.aspx' method='get'><input type='hidden' ID='registerButton' runat='server' name='remove' value='" + prodid + "' /><input type='submit' ID='registerButton' runat='server' value='Remove' /></form></td></tr>";
}
else
{
Response.Redirect("index.aspx", false);
}
删除购物篮项目页
protected void Page_Load(object sender, EventArgs e)
{
int prod = Convert.ToInt32(Request.QueryString["remove"]);
int uid = Convert.ToInt32(Session["uid"]);
String connString = System.Configuration.ConfigurationManager.ConnectionStrings["WebAppConnString"].ToString();
conn = new MySql.Data.MySqlClient.MySqlConnection(connString);
conn.Open();
queryStr = "";
queryStr = "DELETE FROM purchase WHERE ProductID=?prod";
cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn);
//cmd.Parameters.AddWithValue("?uid", uid);
cmd.Parameters.AddWithValue("?prod", prod);
cmd.ExecuteReader();
conn.Close();
Response.Redirect(Request.UrlReferrer.ToString());
}