Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete record

Posted on 2002-06-14
4
Medium Priority
?
348 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 600 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

688 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