Skip to main content
Participant
August 22, 2008
Answered

Error Executing Database Query

  • August 22, 2008
  • 5 replies
  • 824 views
I can't seem to get a simple update cfquery to work (update a table row) without throwing an error in access. I've gone over the code several times and don't understand why it's throwing an error. I want a user to be able to remove themselves from a mailing list, with having to send a reply email to do so.

<!---Who in our database has this email address--->
<cfquery name="getSubscriber" datasource="#request.datasource#" maxrows="1">
SELECT subID, Name, Email
FROM subscribers
WHERE Email= #form.Email#
AND MailingList = 1
</cfquery>

<!---Assuming someone has this email address --->
<CFIF getSubscriber.recordCount eq 0>


<!--- Update the database to remove from mailinglist--->
<cfquery datasource="#request.datasource#">
UPDATE subscribers SET
MailingList = 0
WHERE subID= #getSubscriber.subID#
</cfquery>

Here is the error that is thrown:
Error Executing Database Query. Syntax error (missing operator) in query expression 'Email= user@webserver.com AND MailingList = 1'.

*Note I've intentionally left out the <cfcatch> code since it appears to be working and catching the database error.

So,What gives, what am I doing wrong here with this query?

Thanks in advance for your help.
This topic has been closed for replies.
Correct answer Kronin555
Several problems I see. First, what's this <cfif> checking? What's the first "email" in this check supposed to be? Why don't you just check the recordcount of your select query?
<CFIF Email NEQ ("Form.Email")>

Change to
<cfif getsubscriber.recordcount eq 0>

Then, this cfmail tag... you're setting it to send from the address that was submitted. This is bad form. Have the email come from webmaster or submissions or something, @domainofserver.com, but set reply-to to the submitted email address. Most email servers/spam filters check that the originating mail server has the same domain as the from address, else the message is either dropped or flagged as spam.

Also, your first query, you're now checking where mailinglist = -1, but in your first post, you were checking for mailinglist = 1. Is that right?

5 replies

RocBAuthor
Participant
August 28, 2008
Awesome, I made the change back to <cfif getsubscriber.recordcount eq 0> and it worked like a charm, Guess I had too much of a coffee buzz going and missed it. Thanks again for your valued help.

Regarding the mailinglist = -1 vs. mailinglist=1 , the access database shows either -1 for true or 0 for false. I had not noticed the difference until after I viewed the table data and saw it was -1.

Final thought with regards to the <cfmail> I thought I had the mail coming from the webmaster? I did not include a reply to address because it's only to let the webmaster and site owner (via bcc:) know that a subscriber has canceled their subscription. The end user gets a confirmation on the browser. Nonetheless I'll double check my code and make the necessary corrections.

Thanks again in advance for your reply.
Kronin555Correct answer
Participating Frequently
August 28, 2008
Several problems I see. First, what's this <cfif> checking? What's the first "email" in this check supposed to be? Why don't you just check the recordcount of your select query?
<CFIF Email NEQ ("Form.Email")>

Change to
<cfif getsubscriber.recordcount eq 0>

Then, this cfmail tag... you're setting it to send from the address that was submitted. This is bad form. Have the email come from webmaster or submissions or something, @domainofserver.com, but set reply-to to the submitted email address. Most email servers/spam filters check that the originating mail server has the same domain as the from address, else the message is either dropped or flagged as spam.

Also, your first query, you're now checking where mailinglist = -1, but in your first post, you were checking for mailinglist = 1. Is that right?
RocBAuthor
Participant
August 28, 2008
Thanks for the replies. Okay, so I made the changes and it works, well sort of...(It no longer throws an error) but...
I changed the <CFIF> statement because if I eliminate the first query I get an undefined variable error. After modifying the code it won't update the database regardless if the email address exists or not ?

If you could please review my code and tell me where I am wrong, so I can resolve this, it would be greatly appreciated. Thanks again in advance.

<!---Who in our database has this email address--->
<cfquery name="getSubscriber" datasource="#request.datasource#" maxrows="1">
SELECT subID, Name, Email
FROM subscribers
WHERE Email = <cfqueryparam value="#form.Email#" cfsqltype="cf_sql_varchar" />
AND MailingList = -1
</cfquery>

<!---Assuming someone doesn't have this email address --->
<CFIF Email NEQ ("Form.Email")>
<!--- short status message --->
<cfoutput><p align="center" class="style-bold">Sorry the email address you entered #form.email# was not found? Please check your spelling and re-enter your email address!<br>
</p></cfoutput>
<!--- Display the unsubscribe form --->
<cfform action="#CGI.SCRIPT_NAME#" method="post" style="border:1px solid color:pink;">
<br>
Email:  <input type="text" Value="" Name="Email"><br><br>
<input type="Submit" Value="Unsubscribe">
</cfform>
<CFELSE>
<!--- Update the database to remove from mailinglist--->
<cfquery datasource="#request.datasource#">
UPDATE subscribers
SET MailingList = 0
WHERE Email = <cfqueryparam value="#form.Email#" cfsqltype="cf_sql_varchar" />
</cfquery>
<!--- Show we have removed user from list --->
<cfoutput>
<p class="style2"> Thank you, <br>
Your email address [ #Form.Email# ] Has been removed from our mailing list. You'll not receive another mailing from our site again!</p>
</cfoutput>

<!--- Notify webmaster and owner --->
<cfmail to="webmaster@webserver.com" bcc="name@webserver.com" from ="#Form.Email#" subject="Newsletter Subscriber Cancellation" type="html">
<cfoutput>
#getSubscriber.Name# <#getSubscriber.Email#> <br><br>
Subject: Newsletter cancellation<br><br>
==============================<br>
#Name# < #Email# > Has unsubscribed from any future emails. <br>
We have automatically removed them from our mailinglist. <br>
=============================<br>
Sender IP:#REMOTE_ADDR# <br>
User: #REMOTE_USER#<br>
Browser: #HTTP_USER_AGENT#<br>
Message Sent: #DateFormat(now(), 'mmmm dd, yyyy')# #TimeFormat(Now(), 'hh:mm:ss tt')#
</cfoutput>
</cfmail>

<form name="form1" action="../index.cfm" method="post">
<input type="submit" Value="Continue">
</form>

</CFIF>
Inspiring
August 22, 2008
Kronin answered your specific question. He didn't mention that if your original query returns more than one record, you might not achieve the desirable results.

He probably knew it though.

A better approach would be to skip the first query altogether and move its where clause to the update query.
Participating Frequently
August 22, 2008
You need to surround text fields with single quotes:
WHERE Email= '#form.Email#'

Or, better yet use cfqueryparam:
WHERE Email = <cfqueryparam value="#form.email#" cfsqltype="cf_sql_varchar" />