Server Time:
Thursday May 22 2008 02:01 PM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Delete Multiple Database Records Using A Checkbox
by: Marlene Murphy
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

Delete Multiple Database Records Using A Checkbox SELECT AND DELETE MULTIPLE DATABASE RECORDS WITH A CHECKBOX

Deleting one record or "all" records from a database table is fairly straightforward and there is a good tutorial (#156) on that topic at easyCFM.com. Sometimes, though, to save time you may want to select multiple records for deletion just by checking a box next to the record listing.

This tutorial explains one method of accomplishing this. The first step is pretty easy:

1. Create a SELECT query to list all the records and display them in a table form (just to keep things nice and neat) with a checkbox next to each record. My table is a mailing list. The trick here is to create a unique formfield name for each checkbox created for each displayed record which I'll explain in step 2.

<!--- Get Information from mailing list table --->
<CFQUERY DATASOURCE="#DSN#" NAME="getMailingList">
SELECT memberID, Firstname, Lastname, Email, city, state, country, inputDate, EmailType
FROM tMembers
Order By Lastname, Firstname
</CFQUERY>

<!--- Display Page Header --->
<CFINCLUDE TEMPLATE="adminheader.cfm">

<!--- Create a Form to display the query results and send form info to our action page --->

<CFFORM ACTION="mailingListDelete.cfm" METHOD="POST">
<!--- Outer Container Table for Form (Adds borders and dresses up the display) --->

<TABLE WIDTH="600" BORDER="1" ALIGN="CENTER">
<TR>
<TD>

<!--- Table to hold query info --->
<TABLE WIDTH="600">
<TR>
<TD COLSPAN="8"><DIV ALIGN="CENTER"><B>Mailing List</B></DIV></TD>
</TR>
<TR>
<TD COLSPAN="8"><DIV ALIGN="CENTER">

<CFOUTPUT>#getMailingList.RecordCount#</CFOUTPUT> Mailing List Members<BR>
To remove person from mailing list, select the checkbox
next to their name and click Delete Members</DIV></TD>
</TR>

<!--- Header row to display column names --->
<TR>
<TH>Del</TH>
<TH>Name</TH>
<TH>City</TH>
<TH>State</TH>
<TH>Country</TH>
<TH>Email</TH>
<TH>Date Reg.</TH>
<TH>Email</TH>
</TR>


<CFOUTPUT QUERY="getMailingList">

2. The first column in my table includes a checkbox with a unique name tied to my primary key, called "memberID". I've named the checkbox "DEL_#memberID#". As the output query goes through each item, it will add the unique memberID to DEL_, which means I'll end up with unique checkbox form names. For example, DEL_145 (where the member ID is "145"). When we get to the action page, we'll trim off the beginning and use the memberID to select the records to delete.


<TR>
<TD>
<INPUT TYPE="checkbox" NAME="DEL_#memberID#" VALUE="YES"></TD>
<TD>#LastName#, #FirstName#</TD>
<TD>#City#</TD>
<td>#State#</td>
<TD>#Country#</TD>
<td>#Email#</td>
<td>#DateFormat(inputDate,"mm/dd/yy")#</td>
<td>#EmailType#</td>
</TR>
</CFOUTPUT>
<tr>
<td colspan="8">
<INPUT TYPE="submit" VALUE="Delete Members"></td>

</tr>
</TABLE></TD></TR></TABLE>
</CFFORM>
</body></html>


Okay...that takes care of our mailing list form table with the checkboxes. Now we need to create our action page, called "mailingListDelete.cfm". We're going to loop through all of the form field names (actually, the only formfield we have is called DEL_#memberID#).

3. Verify that a record was selected for deletion and a field name was passed to the action page. In case someone clicked "Delete Members" without selecting a record to delete, we need to bypass the delete procedure and display a message to the user.

<CFIF IsDefined("Form.FieldNames")>


4. We know that each Checkbox formfield begins with DEL_ (which is 4 characters), so we're going to remove the first four characters from the formfield name with the RemoveChar function to end up with the memberID.

<!--- This loop pulls the memberID from the Delete FieldName --->
<!--- then runs the delete query for that memberID --->

<cfloop index="i" list="#FORM.FieldNames#" DELIMITERS="," >
<cfif LEFT(i, 4) IS "DEL_" >

<CFSET selectMemberID = RemoveChars(i, 1, 4)>
<CFSET selectMemberID = #Evaluate(selectMemberID)#>
</cfif>


<!--- Delete Query for selected member IDs --->
<CFQUERY DATASOURCE="#DSN#" NAME="deleteMembers">
DELETE
FROM tMembers
WHERE memberID = #SelectMemberID# [SEE TUTORIAL UPDATE AT BOTTOM OF PAGE]
</CFQUERY>
</cfloop>

5. That's it! Now I'm just going to display a message to the user that the selected records have been deleted, or if no records were selected by checking the checkbox, I'm going to tell them that.

<!--- Display Page Header --->
<CFINCLUDE TEMPLATE="adminheader.cfm">

<!--- Table to display success message to user --->
<table WIDTH="600" HEIGHT="50%" ALIGN="CENTER">
<tr>
<td ALIGN="CENTER"> <H2>SELECTED MAILING LIST MEMBERS DELETED! </H2>
<H2><A HREF="mailingList.cfm">DELETE MORE MEMBERS</A></H2></td></tr></table>


<CFELSE>
<!--- Table to display message to user that no records were selected for deletion --->

<!--- Display Page Header --->
<CFINCLUDE TEMPLATE=
"adminheader.cfm">
<table WIDTH="600" HEIGHT="50%" ALIGN="CENTER">
<tr>
<td ALIGN="CENTER"> <H2>NO MAILING LIST MEMBERS SELECTED FOR DELETION </H2>
<H2><A HREF="mailingList.cfm">GO TO MEMBERLIST</A></H2></td></tr></table>

</CFIF>
</body></html>


TUTORIAL UPDATES 03/11/04:

Since this tutorial was originally submitted, a couple of issues have arisen which need to be addressed:
1. If the fields you are using to select a record to delete are NOT numerical, but are textfields, you must be sure to put single quotes around your variables. For example, if your product ID includes alpha-numeric characters, you need to add quotes:

WHERE productID = '#SelectproductID#'

2. It was brought to my attention by another Forum member that my original tutorial included a name for the "submit" button, which would produce an error, so I have removed the name= variable from the submit button in this tutorial. Sorry if it caused anyone problems!

 


Date added: Tue. February 10, 2004
Posted by: Marlene Murphy | Views: 12664 | Tested Platforms: CFMX | Difficulty: Intermediate
Categories Listed: Databases

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Comments on this tutorial
Read previous comments on this particular tutorial
Variable Undefined
I recreated the cfm pages exactly as they are shown above and every time the delete actions page runs it returns a
"Variable SELECTMEMBERID is undefined" Can anyone help?
Posted by: drake
Posted on: 04/21/2004 12:15 PM
Variable Undefined
i'm also got the same problem like drake.. So anyone can help us..
Posted by: dazza
Posted on: 04/21/2004 10:27 PM
variable undefined
yeah the problem is that you probably have a space between selectmemberID either in your query or code.. i did and that was the problem.
Posted by: mac
Posted on: 05/16/2004 12:49 PM
more efficient way...
sorry, but a more efficient way would be to use a checkbox like so...

form.cfm
loop-age {
<input type="checkbox" name="deleteitemid" value="#itemID#">
}

processing.cfm
<CFLOCK NAME="item" TYPE="Exclusive" TIMEOUT="30">
<CFQUERY DATASOURCE="#request.dsn#">
DELETE from Sometbl
WHERE itemid IN (#form.deleteitemid#)
</CFQUERY>
</CFLOCK>

Essentially, this way, you can actually specify the name of the form element to be used in the delete query, therefore being more efficient, instead of looping through other form elements (if you have em), also, by using the IN keyword you can pass a comma delimited list to the delete statement therefore alleviating the looping of all form.elements and re-querying of the db.

So now you can have a efficient way to accomplish the same task.
Posted by: matt
Posted on: 06/20/2004 07:18 PM
How do I delete from Two Table
I'm doing something a little different, can anyone help?
Here is the deal..... I have two tables, one for the company address information named "company" and another related table for company employees contact information, named "employee"

What I'm trying do is when I delete a company from the company table, I also want to delete all the employees from the employee table associated with the company being deleted.

I setup the "empoyee" database to have the same ID as the company record. So the ID for every contact being delete in the employee database is the same as the company ID being deleted. I have a feeling I'm just messing up the Sql statement. Here is what I have below.

I assume all the magic takes place on the page you labeled mailingListDelete.cfm in your tutorial.

<CFIF IsDefined("Form.FieldNames")>

<cfloop index="i" list="#FORM.FieldNames#" DELIMITERS="," >
<cfif LEFT(i, 4) IS "DEL_" >

<CFSET selectID = RemoveChars(i, 1, 4)>
<CFSET selectID = #Evaluate(selectID)#>
</cfif>

<CFQUERY DATASOURCE="database" NAME="deleteMembers">
DELETE
FROM recruiters
WHERE ID = #selectID#
</CFQUERY>

<CFQUERY DATASOURCE="database" NAME="deleteMembers2">
DELETE
FROM Employee
WHERE ID = #selectID#
</CFQUERY>

</cfloop>

Do you need two cfquries? Or can this be accomplished with one statement? Or does something else need to be defined in the <cfset>?

Any help would be greatly appreciated.
Posted by: Bill
Posted on: 08/23/2005 12:14 AM
no comments
keep it up...
Posted by: marc justin
Posted on: 02/15/2006 10:06 AM
Taste Great! Less Filling!
I tired both ways and both seem to work well in my development box. Matt's was a little easier to understand since you don't have to remove characters (and would result in faster execution due to less hits to the server), but Marlene's could be setup in such a way to allow for flagging entries to update or perhaps move users to an archived or inactive user list for example. Basically both examples have their place.
Posted by: Daryl Lackey
Posted on: 04/02/2006 05:37 AM
Go for effieciency
Please people - use Matt's example as it doesn't query the database numerous times - it only uses one query and gets the database to do all the hard work instead of CF.
Posted by: Neil Merton
Posted on: 07/31/2006 09:18 AM
error
I used createUUID for my id names so I've got dashes in in the them (selectMemberID). problem is this cuts the name off at the first dash and only queries the first few number before the first dash.

For example, if memberID = 'DD9AF668-B6C9-77CB-B454B2358574BED8'

then an error returns:'DD9AF6680' on line 1, column 1, is not a valid identifer name.
Posted by: jwerre
Posted on: 02/27/2007 01:25 AM
hey
how do u do this for an update? looping?
Posted by: me123
Posted on: 06/24/2007 10:55 PM
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
Daily Razor - ColdFusion Hosting

You are 1 of 570 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved (Server: www0002)
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb