Do you mean that all the code on one page (input.cfm)?
If so, this about it logically. The addDonor query is processed every time
the page is loaded- once where the user enters the data (blank data except
for the PK) and a second time (with the user entered data).
You need a conditional around the query to only fire if the form has been
submitted:
<cfif IsDefined("FORM")>
<cfquery name="addDonor" datasource="test"> INSERT INTO DONOR
(first,last,flag,supe,phone) VALUES
('#form.first#','#form.last#','#form.flag#','#form.supe#','#form.phone#')
</cfquery>
</cfif>
Additionally, you need to do some data scrubbing before the cfquery to
prevent SQL injection.
- Dave
-----Original Message-----
From: [email protected] [mailto:[email protected]] On
Behalf Of B Griffith
Sent: Tuesday, January 15, 2013 12:34 PM
To: cf-talk
Subject: form-post to SQL-insert creates double-entry
Hello All,
I appreciate everyone's help on my other posts and think I'm coming to
understand CF a lot better than I did before I started posting here a few
days ago, so kudos to you, friends!
My latest issue w/ my burgeoning new website is the input.cfm page, where an
end-user may (in theory) enter a donor's information into a webform, click
submit, and the data will subsequently be inserted into the SQL Server
(2005) table dbo.DONOR, which while it's under development, has only six
fields: kcid (auto-incrementing PK/UID for the database) and the other five
which you see in the code below and are probably pretty self-explanatory. I
have tried creating an empty structure called 'form' before applying the
code and finally settled on a block of <cfparam> statements to set the vars
to a default. I can't see a way around this because NOT doing so causes the
form to indeed show at the top of the webpage, but it is followed by a CF
error page. INCLUDING the <cfparam> tags or <cfset var = ""> or form =
structNew(); causes the error output to go away but results in TWO
recordsets being sent to the DB, one blank (except for the PK which is set
up to auto-increment in SQL) and one with the correct info, and taking the
next PK number. I also tried setting the fields to NOTNULL = TRUE in SQL
but to no avail, apparently "" translates into something besides null on the
server-side. Here is the code, I would greatly appreciate your thoughts on
what I'm doing wrong and how to rectify it:
<html>
<head>
<title>Input form for new donor</title>
</head>
<cfparam name="form.first" default="">
<cfparam name="form.last" default="">
<cfparam name="form.flag" default="">
<cfparam name="form.supe" default="">
<cfparam name="form.phone" default="">
<body>
<div> <hr /> </div>
<form action="input.cfm" method="post">
<table>
<tr>
<td>Donor First Name:</td>
<td><input type="text" name="first" id="first" value="" /></td>
</tr>
<tr>
<td>Donor Last Name:</td>
<td><input type="text" name="last" id="last" value="" /></td>
</tr>
<tr>
<td>Donor's Immediate Supervisor:</td>
<td><input type="text" name="supe" id="supe" value="" /></td>
</tr>
<tr>
<td>Supervisor's Phone Number:</td>
<td><input type="text" name="phone" id="phone" value="" /></td>
</tr>
<tr>
<td>Has this employee provided a sample in the past 30 days?
(Y/N):</td>
<td><input type="text" name="flag" id="flag" value="" /></td>
</tr>
</table>
<input type="submit" value="Enter New Donor">
<br /><a href="index.cfm">Back to homepage</a>
<br /><a href="random.cfm">Random Donor Generator</a>
</body>
<cfquery name="addDonor" datasource="test"> INSERT INTO DONOR
(first,last,flag,supe,phone) VALUES
('#form.first#','#form.last#','#form.flag#','#form.supe#','#form.phone#')
</cfquery>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive:
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:353893
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm