65.9K
CodeProject 正在变化。 阅读更多。
Home

使用复选框更新多个数据库条目

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (11投票s)

2002年3月11日

3分钟阅读

viewsIcon

167897

downloadIcon

1757

此代码允许您使用复选框更新任意数量的记录,这些记录具有是/否类型的值

引言

我看到很多人都在努力解决这类问题,所以在为我的一个朋友编写了一个解决方案之后,我决定让人们看到这种做事方式是有用的。

作为一个例子,我将保持事情非常简单,问题是这样的
你有一个表(在本例中称为aTable),其中有一个名为id的主键列和一个名为active的位(是/否,用于访问人员)字段。 你想做的是显示一个form,其中aTable中的每个记录都有一个checkbox,并允许用户在一个提交中更新每个记录的active字段(如果勾选了checkbox,则active将被设置为1,如果未勾选则设置为0)。 显然,您可以通过让每个记录都在自己的form中来简化事情,但这会使更新大量记录成为真正的痛苦。

这是我对这个问题的解决方案。 下面是为form生成的HTML示例

<form name="form1" action="chkBoxes.asp" method="post">
 <table>
  <tr>
   <td>Checkbox1:</td>
   <td><input type="checkbox" name="chkBox" value="1"></td>
  </tr>
  
  <tr>
   <td>Checkbox2:</td>
   <td><input type="checkbox" name="chkBox" value="2"></td>
  </tr>
  
  <tr>
   <td>Checkbox3:</td>
   <td><input type="checkbox" name="chkBox" value="3"></td>
  </tr>
  
  <tr>
   <td>Checkbox4:</td>
   <td><input type="checkbox" name="chkBox" value="4"></td>
  </tr>
  
  <tr>
   <td>Checkbox5:</td>
   <td><input type="checkbox" name="chkBox" value="5"></td>
  </tr>
  <tr>
   <td colspan="2"><input type="submit" value="go"></td>
  </tr>
 </table>
 <input type="hidden" name="allBoxes" value="1,2,3,4,5">
</form>

checkboxes的值将被设置为aTable中的id。 正如你所看到的,所有的checkboxes都被命名为相同的东西。 这简化了检索结果,否则我们将不得不进行一些动态request.form调用,这些调用看起来非常难看。

好的,这就是我们的form,现在我们该怎么办呢? 让我们看看第一段代码。

'get the array with all of the checkbox id's

arrAll = split(Request.Form ("allBoxes"), ",")
 
'get the array with the id's that were ticked

arrVals = split(Request.Form ("chkBox"), ",")

我们在这里所做的就是用来自我们form的结果填充两个数组。 第一个数组arrAllallBoxes hidden input填充,正如你在form代码中看到的那样,这是一个包含checkboxes所有值的字符串。 第二个数组arrVals用被选中的checkboxes填充(记住,如果一个checkbox没有被选中,它不会在form post中提交一个值。)。 到目前为止,一切都很简单,现在我们生成SQL statement,它更新被选中的值。

strSql = ""
strSql = strSql & "UPDATE aTable SET active = 1 WHERE "

'loop through the checkboxes which were ticked

for i = 0 to ubound(arrVals)
 if i = 0 then
  strSql = strSql & "id = "& arrVals(i)
 else
  'only add the " AND " if this is not the first value

  strSql = strSql & " AND id = "& arrVals(i)
 end if
next

Response.Write strSql & "<hr>"

我们在这里所做的只是简单地循环遍历包含在arrVals数组中的checkbox values。 如果你还记得,这些是被选中的值,所以SQL statement正在将active列设置为1(即使其处于活动状态)。 仍然非常简单,接下来我们需要生成SQL statement,它将未选中的值标记为不活动。

'dimension the array to the size we need

redim arrInActive(ubound(arrAll) - ubound(arrVals))

'set our indexer variable

IDX = 0

'loop through all the checkbox values

for i = 0 to ubound(arrAll)
 'a boolean value to check if we match or not

 bThere = false
 'loop through the values which were submitted

 for z = 0 to ubound(arrVals)
  'if it is found then set the boolean to true

  'this is so we don't add it to the new array

  if trim(arrVals(z)) = trim(arrAll(i)) then
   bThere = true
  end if
 next 
        'if it wasn't in the submitted array (i.e. it wasn't checked)

 if bThere = false then 
  'add the value to the new array

  arrInactive(IDX) = arrAll(i)
  'increment our indexer

  IDX = IDX + 1
 end if
next

strSql = ""
strSql = strSql & "UPDATE aTable SET active = 0 WHERE "
'loop through the array which holds the values that were NOT ticked

for i = 0 to ubound(arrInactive)
 if arrInactive(i) <> "" then
  if i = 0 then
   strSql = strSql & "id = "& arrInactive(i)
  else
   'only add the " ADD " if this is not the first variable

   strSql = strSql & " AND id = "& arrInactive(i)
  end if
 end if
next

Response.Write strSql

这段代码有点复杂。 首先是定义一个数组来存储未选中的值。 基本上,接下来要做的就是循环遍历包含在arrAll数组中的所有值,然后对于任何未出现在arrVals数组中的值(即它们未被选中),将它们添加到arrInactive数组中。 最后,我们循环遍历这个新填充的数组以生成SQL statement

查看代码中的注释以更好地解释每段代码。

您应该发现这段代码很容易修改,以便您可以将其与数据库一起使用,我选择这样编写是为了使概念更清晰。 如果您在数据库上下文中实现此代码时遇到困难,请告诉我,我会尽力帮助您。

我希望这可以帮助一些人解决这个潜在的棘手问题。 祝你好运,一如既往地欢迎提出建议和建设性批评。

灵感来自 Bill Wilkinson 在 aspFaqs.com 上的文章。 我认为 Bill 的方法有点过于复杂,所以我决定发布我的解决方案(虽然 Bill 的文章可能写得更好)。

© . All rights reserved.