Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to use a check box - yes/no in a form to update db

Participant ,
Mar 23, 2009 Mar 23, 2009
Hello;
I am trying to use a checkbox in a form I created. I need to for a yes or no feature. If it's checked, it is yes, if not, no.

I have it in a form that can either add a "user" or update an existing record. Right now it is throwing an error, and it is for the check box code. Here is the code that matters for this and the error:

page userEdit.cfm

<cfparam name="url.RecordID" type="integer" default="0">
<cfparam name="variables.ID" type="integer" default="#url.RecordID#">
<cfparam name="variables.Fname" default="">
<cfparam name="variables.Lname" default="">
<cfparam name="variables.userName" default="">
<cfparam name="variables.password" default="">
<cfparam name="variables.email" default="">
<cfparam name="variables.admin" default="">

<cfif url.RecordID GT 0>
<cfquery name="useRec" datasource="#APPLICATION.dataSource#">
SELECT user.Fname, user.Lname, user.userName, user.password, user.email, user.admin, user.ID
FROM user
WHERE user.ID =<cfqueryparam value="#URL.RecordID#" cfsqltype="cf_sql_integer">
</cfquery>

<cfif useRec.RecordCount EQ 1>
<cfset variables.RecordID = useRec.ID>
<cfset variables.Fname = useRec.Fname>
<cfset variables.Lname = useRec.Lname>
<cfset variables.userName = useRec.userName>
<cfset variables.password = useRec.password>
<cfset variables.email = useRec.email>
<cfset variables.admin = useRec.admin>
</cfif>
</cfif>
<head>
......
<body>
<cfoutput>
<cfform action="action.cfm" method="post">
<cfinput type="hidden" name="ID" value="#variables.ID#">
..... more for feilds here ..........
<cfinput type="checkbox" name="admin" value="#variables.admin#">
... submit buttons here ...
</cfform>
</body>

Action Page:

<cfif isDefined("Form.RecordID")>
<cfquery datasource="#APPLICATION.dataSource#">
UPDATE user
SET user.Fname=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Fname#">,
user.Lname=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Lname#">,
user.userName=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.userName#">,
user.password=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.password#">,
user.email=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.email#">,
user.admin=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.admin#">
WHERE ID =<cfqueryparam value="#form.RecordID#" cfsqlType="CF_SQL_INTEGER">
</cfquery>
<cflocation url="indexUser.cfm?RecordID=#Form.RecordID#" addtoken="no">
<cfelse>
<cfquery result="GetNewRecord" datasource="#APPLICATION.dataSource#" dbtype="ODBC">
INSERT INTO user
(Fname, Mname, Lname, userName, password, email, admin)
VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Fname#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Lname#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.userName#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.password#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.email#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.admin#">)
</cfquery>
<cflocation url="indexUser.cfm">
</cfif>

My error message:

Error Executing Database Query. [Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Number of query values and destination fields are not the same.
The error occurred on line 33.

That is the line where it is supposed to place the checkbox into teh database on the action page.
How do I use a checkbox for this? The checkbox is the admin tab in the db and that is set to a yes/no setting

Thank you.

CFmonger
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 24, 2009 Mar 24, 2009
quote:

Originally posted by: CFmonger
I fixed the error, it isn't throwing that anymore. It makes a new record now when I check off the check box.

I think it probably has to be with me scoping what that checkbox will do when it updates or adds a new record to the database? Correct? That is why it is adding a new record instead of just checking the yes/no in the database table.

If this is my problem, then how do I scope this?

That is not your problem. The code in the OP shows the update an...
Translate
LEGEND ,
Mar 23, 2009 Mar 23, 2009
submit your form with the check box checked and unchecked. cfdump your form on the action page.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 23, 2009 Mar 23, 2009
I can't get it to dump, I tried
<cfdump var="#form#">
<cfabort>

it wouldn't fire. BUT I did get it to not throw an error. but now, if you heck the box, it makes another record. I just need it to hit yes / no in the db. How do I get this to work properly, the only change in my code is this:

<cfquery result="GetNewRecord" datasource="#APPLICATION.dataSource#" dbtype="ODBC">
INSERT INTO user
(Fname, Lname, userName, password, email, admin)

I had to take out Mname, I did away with that.

I have been trying to find out how to program in a check box like this, I have the books, and have been searching live docs and can't find a solid answer.

How do I do it?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 23, 2009 Mar 23, 2009
Let's look at that error message again.
Error Executing Database Query. [Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Number of query values and destination fields are not the same.

Did you count anything in your insert query?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 23, 2009 Mar 23, 2009
I fixed the error, it isn't throwing that anymore. It makes a new record now when I check off the check box.

I think it probably has to be with me scoping what that checkbox will do when it updates or adds a new record to the database? Correct? That is why it is adding a new record instead of just checking the yes/no in the database table.

If this is my problem, then how do I scope this?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 24, 2009 Mar 24, 2009
quote:

Originally posted by: CFmonger
I fixed the error, it isn't throwing that anymore. It makes a new record now when I check off the check box.

I think it probably has to be with me scoping what that checkbox will do when it updates or adds a new record to the database? Correct? That is why it is adding a new record instead of just checking the yes/no in the database table.

If this is my problem, then how do I scope this?

That is not your problem. The code in the OP shows the update and insert query governed by the same if/else logic.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 24, 2009 Mar 24, 2009
LATEST
I think I have it 90%there. I have no error now, it edits a record fine, if you add the checkbox to the record it adds it, and if you take it away, it does that. BUT now it will not add a new record.

This is my code now for the update / insert

<cfif not IsDefined("FORM.admin")>
<cfset IsAdmin= 0>
<cfelse>
<cfset IsAdmin= 1>
</cfif>
<cfif isDefined("Form.RecordID")>
<cfquery datasource="#APPLICATION.dataSource#">
UPDATE user
SET user.Fname=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Fname#">,
user.Lname=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Lname#">,
user.userName=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.userName#">,
user.password=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.password#">,
user.email=<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.email#">,
user.admin =<cfqueryparam value="#IsAdmin#" CFSQLType="CF_SQL_INTEGER">
WHERE ID =<cfqueryparam value="#form.RecordID#" cfsqlType="CF_SQL_INTEGER">
</cfquery>
<cflocation url="indexUser.cfm?RecordID=#Form.RecordID#" addtoken="no">
<cfelse>
<cfquery result="GetNewRecord" datasource="#APPLICATION.dataSource#" dbtype="ODBC">
INSERT INTO user
(Fname, Lname, userName, password, email, admin)
VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Fname#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.Lname#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.userName#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.password#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.email#">,
<cfqueryparam value="#IsAdmin#" CFSQLType="CF_SQL_INTEGER">)
</cfquery>
<cflocation url="indexUser.cfm">
</cfif>

Is there a better way to write this so it will work? I have everything else working including the checkbox showing checked if it is checked in the db, I believe this is my last hang up on a checkbox.

Any ideas?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources