Solved

Delete record

Posted on 2002-06-14
4
333 Views
Last Modified: 2013-12-24
I am developing a guestbook for my site which I am trying to build right now. I have gotten the book up and running, including submitting and maintenance stuff like changing messages to get rid of some bad stuff in there. Now I only have a problem with deleting a record from the database in the same maintenancepage as changing it. There are 3 pages right now. I have put the code of both pages in here. The Delete button is already there, but not working yet.

Page 1: Get a list out of the database by name.
<html>
<head>
     <title>Maintenance List</title>
</head>
<body>
<cfquery name="GetMessages" datasource="GuestBook">
     SELECT FirstName, LastName, Message_ID
     FROM GuestBook
</cfquery>
<cfoutput query="GetMessages">
     <a href="MaintenanceForm.cfm?Message_ID=#GetMessages.Message_ID#">#FirstName# #LastName#</a><br>
</cfoutput>
</body>
</html>


Page 2: The Maintenance Form
<html>
<head>
<title>
     Guestbook Andre and Yulya
</title>
</head>
<body>
<cfquery name="GetMessageDetails" datasource="GuestBook">
     SELECT Message_ID, FirstName, LastName,
               HomeTown, Country, HomePage, Comment
     FROM GuestBook
     WHERE Message_ID = #URL.Message_ID#
</cfquery>
<form action="MaintenanceActionForm.cfm" method="post">
  <input type="Hidden" name="Message_ID" value="<cfoutput>#URL.Message_ID#</cfoutput>">
               <table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>
<font face="Comic Sans MS" color="Red">
First Name:
</font>
</td>
<td>
<input type="Text" name="FirstName" size="30" maxlength="30" value="<cfoutput>#GetMessageDetails.FirstName#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Last Name:
</font>
</td>
<td>
<input type="Text" name="LastName" size="30" maxlength="30" value="<cfoutput>#GetMessageDetails.LastName#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Home Town:
</font>
</td>
<td>
<input type="Text" name="HomeTown" size="30" maxlength="30" value="<cfoutput>#GetMessageDetails.HomeTown#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Country:
</font>
</td>
<td>
<input type="Text" name="Country" size="30" maxlength="30" value="<cfoutput>#GetMessageDetails.Country#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Homepage URL:
</font>
</td>
<td>
<input type="Text" name="HomePage" size="70" maxlength="70" value="<cfoutput>#GetMessageDetails.HomePage#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Comment:
</font>
</td>
<td>
<textarea cols="80" rows="4" name="Comment"><cfoutput>#GetMessageDetails.Comment#</cfoutput>"></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<center>
<input type="Submit" name="Submit" value="Change">
<input type="Reset" name="Reset" value="Reset">
<input type="Button" name="Delete" value="Delete">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>


Page 3: The Maintenance Action Form
<html>
<head>
     <title>Maintenance Action Form</title>
</head>
<body>
<cfquery name="UpdateMessage" datasource="GuestBook">
     UPDATE GuestBook
     SET FirstName='#Form.FirstName#',LastName='#Form.LastName#', HomeTown='#Form.HomeTown#', Country='#Form.Country#', HomePage='#Form.HomePage#', Comment='#Form.Comment#'
     WHERE Message_ID=#Message_ID#
</cfquery>
</body>
</html>
0
Comment
Question by:Dreetje
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
hammond_david earned 200 total points
ID: 7082040
The simplest way to do this would be to make the delete button a submit button:

<input type="Submit" name="Delete" value="Delete">

When the button is pressed the form will be submitted and the form parameter "Delete" will be sent to the action page.  The parameter will only be sent if the button is clicked, so on the action form you can put:

<cfif IsDefined("form.Delete")>
  <cfquery name="DeleteMessage" datasource="GuestBook">
    Delete from GuestBook
    WHERE Message_ID=#Message_ID#
  </cfquery>
<cfelse>
  <!--- update --->
</cfif>

You probably want to add some confirmation logic to safeguard against someone hitting the delete button accidentally.  You can do this with javascript on the form page:

<input type="Submit" name="Delete" value="Delete" onclick="return confirm('This record will be permanently deleted.  Continue?')">

Hope that helps
- David
0
 
LVL 17

Expert Comment

by:anandkp
ID: 7174065
HI there,

This is simple -

Ur maintenance page has the code for only updating a Table record & so no matter what u do - it would never delete ur record.

what u need to do is pass a parameter when u delete

like say on delte send Act="del"
remember Act is a varible passed thru ur form action

I'll modify ur code hereitself - wait ....

======================================================

Page 2: The Maintenance Form
<html>
<head>
<title>
    Guestbook Andre and Yulya
</title>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--

function delme()
{
window.document.frm.action = "MaintenanceActionForm.cfm?Act=Del"
window.document.frm.submit();
}

//-->
</SCRIPT>
</head>
<body>
<cfquery name="GetMessageDetails" datasource="GuestBook">
    SELECT Message_ID, FirstName, LastName,
              HomeTown, Country, HomePage, Comment
    FROM GuestBook
    WHERE Message_ID = #URL.Message_ID#
</cfquery>

<!-- always give ur form a name -->

<form name="frm" action="MaintenanceActionForm.cfm" method="post">
 <input type="Hidden" name="Message_ID" value="<cfoutput>#URL.Message_ID#</cfoutput>">
              <table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>
<font face="Comic Sans MS" color="Red">
First Name:
.
.
.
.

==============================================


Page 3: The Maintenance Action Form
<html>
<head>
    <title>Maintenance Action Form</title>
</head>
<body>
<cfif isdefined('Act')>

<cfquery name="deleteMessage" datasource="GuestBook">
    delete GuestBook
    WHERE Message_ID=#Message_ID#
</cfquery>

<cfelse>

<cfquery name="UpdateMessage" datasource="GuestBook">
    UPDATE GuestBook
    SET FirstName='#Form.FirstName#',LastName='#Form.LastName#', HomeTown='#Form.HomeTown#', Country='#Form.Country#', HomePage='#Form.HomePage#', Comment='#Form.Comment#'
    WHERE Message_ID=#Message_ID#
</cfquery>

</cfif>

</body>
</html>

0
 
LVL 17

Expert Comment

by:anandkp
ID: 7174071
Sorry ignore the earlier one !!!

HI there,

This is simple -

Ur maintenance page has the code for only updating a Table record & so no matter what u do - it would never delete ur record.

what u need to do is pass a parameter when u delete

like say on delte send Act="del"
remember Act is a varible passed thru ur form action

I'll modify ur code hereitself - wait ....

======================================================

Page 2: The Maintenance Form
<html>
<head>
<title>
    Guestbook Andre and Yulya
</title>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--

function delme()
{
window.document.frm.action = "MaintenanceActionForm.cfm?Act=Del"
window.document.frm.submit();
}

//-->
</SCRIPT>
</head>
<body>
<cfquery name="GetMessageDetails" datasource="GuestBook">
    SELECT Message_ID, FirstName, LastName,
              HomeTown, Country, HomePage, Comment
    FROM GuestBook
    WHERE Message_ID = #URL.Message_ID#
</cfquery>

<!-- always give ur form a name -->

<form name="frm" action="MaintenanceActionForm.cfm" method="post">
 <input type="Hidden" name="Message_ID" value="<cfoutput>#URL.Message_ID#</cfoutput>">
              <table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>
<font face="Comic Sans MS" color="Red">
First Name:
.
.
.
.

</td>
</tr>
<tr>
<td>
</td>
<td>
<center>
<input type="Submit" name="Submit" value="Change">
<input type="Reset" name="Reset" value="Reset">
<input type="Button" name="Delete" value="Delete" onclick="javascript:delme()">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>



==============================================


Page 3: The Maintenance Action Form
<html>
<head>
    <title>Maintenance Action Form</title>
</head>
<body>
<cfif isdefined('Act')>

<cfquery name="deleteMessage" datasource="GuestBook">
    delete GuestBook
    WHERE Message_ID=#Message_ID#
</cfquery>

<cfelse>

<cfquery name="UpdateMessage" datasource="GuestBook">
    UPDATE GuestBook
    SET FirstName='#Form.FirstName#',LastName='#Form.LastName#', HomeTown='#Form.HomeTown#', Country='#Form.Country#', HomePage='#Form.HomePage#', Comment='#Form.Comment#'
    WHERE Message_ID=#Message_ID#
</cfquery>

</cfif>

</body>
</html>

0
 

Author Comment

by:Dreetje
ID: 7189066
It helped a lot. I was almost there solving it myself, but I needed just the last peice! Yhanks, and I htink it was an easy one or someone who uses CF more often :-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now