Solved

Delete record

Posted on 2002-06-14
4
337 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

New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
website file permissions 4 65
cloud web Service looking for a home... 3 100
Setting Up Local Lan Web Server 1 50
DNS @ Naked Domain Record 5 69
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…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

22 Experts available now in Live!

Get 1:1 Help Now