Solved

Delete record

Posted on 2002-06-14
4
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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