| Need to Perform Multiple Updates? |
|---|
| While this article shows how to use a single HTML form to delete multiple records from a database, you may be wondering how to allow a user to update multiple database entries via one form / Web page. To find the answer, look no further than: |
Chances are you've seen a web page that lists several records from a database, with a checkbox next to each record. You can easily delete a number of records at once by simply checking the records you'd like to delete, and then clicking a submit button. Believe it or not, but to create such a page using ASP is painfully simple!
In a previous article we talked about SQL Set Notation. Using set notation when working with SQL allows you to process a number of rows in just one statement. If you haven't read this article, I highly suggest you do so now. If you have a table that has a unique ID, you can use the following single SQL statement to delete a number of records:
DELETE FROM TableName
|
If the comma-delimited list contains seven IDs in the table, seven records will be deleted. For our
example, I created a simple Access database containing one table, Products. Products
contains the following definition:
| Products | |
|---|---|
| ProductID | Autonumber |
| Name | Text |
| Cost | Currency |
Now, to create our web page that lists all of the items in the Products table, with a
"delete checkbox" next to each, we will use the following code (ListProducts.asp):
<%@ Language=VBScript %>
|
The code is fairly self-explanatory. We open up a recordset object of the table Products
and loop through each record in the table. We create a checkbox next to each product. The screen shot to
the right shows what the output of ListImages.asp looks like with some same data.
Note that we give every single checkbox the same NAME. This passes to DeleteProducts.asp
the VALUEs of the checked checkboxes in a comma-delimited list! Needless to say, we set the
VALUEs of our checkboxes to the ProductID, our unique identifier!! All we need
to do now is write the DeleteProducts.asp:
<%@ Language=VBScript %>
|
Pretty neat, eh!? This will delete all of the checked products. If no products are checked, a message will be displayed, indicating that no checkboxes were checked.
Happy Programming!
Attachments:
ListProducts.asp in text formatDeleteProducts.asp in text format



