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

RecordCount Question

New Here ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

Greetings

I don't know why this is not inserting the data if the maximum number of records (seats at a conference) has not been reached:

<CFQUERY NAME="count_num" DATASOURCE="whatever">
SELECT attend_session
FROM main
WHERE attend_session = #Form.attend_session#
</cfquery>

<cfoutput query="count_num">

<cfif #count_num.RecordCount# GTE 30>
<cflocation url="full.cfm" addtoken="No">

<cfelse>


<cfquery name="add_attendee" datasource="whatever">
INSERT INTO main
VALUES ('#Form.attend_lname#',
'#Form.attend_fname#',
#Form.attend_session#)
</cfquery>
Thank you...etc.
</cfif></cfoutput>

Any help with this otherwise simple task :>( would be appreciated.
TOPICS
Advanced techniques

Views

674

Translate

Translate

Report

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
Guide ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

Instead of returning all records in the table you can count the total seats like this

<!--- or possibly SELECT COUNT(*) AS NumberAttending .... --->
<CFQUERY NAME="count_num" DATASOURCE="whatever">
SELECT COUNT(Attend_Session) AS NumberAttending
FROM main
WHERE attend_session = #Form.attend_session#
</CFQUERY>

You don't need to loop through the query. Just use

<cfif count_num.NumberAttending LT 30>
do the insert
<cfelse>
<cflocation url="full.cfm" addtoken="No">
</cfif>

Though you should use cfqueryparam and use a column list in your insert (if you're not doing that already)

Votes

Translate

Translate

Report

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
New Here ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

Thanks- this works now...

It did not work at first using the <cfquery>INSERT INTO but works fine using <CFINSERT>

Don't know why but thanks again.

rinorman

Votes

Translate

Translate

Report

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
Guide ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

What was the error?

Personally I would avoid CFINSERT. A regular sql INSERT is the way to go.

Votes

Translate

Translate

Report

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
New Here ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

Probably my mistake in the INSERT (see in first message) but the error is:
"Number of query values and destination fields are not the same. "

Votes

Translate

Translate

Report

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
Guide ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

Use a column list in your insert statement. In other words, explicitly specify which values you're inserting into which columns. Its not always required, but its a good practice. It helps avoid inserting values into the wrong columns.

INSERT INTO main ( TheLastNameColumn, TheFirstNameColumn, TheSessionColumn)
VALUES ('#Form.attend_lname#', '#Form.attend_fname#', #Form.attend_session#)

Votes

Translate

Translate

Report

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
Guide ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

Also, consider using <cfqueryparam> for your form values.

Votes

Translate

Translate

Report

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 ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

quote:

Originally posted by: newportri
Probably my mistake in the INSERT (see in first message) but the error is:
"Number of query values and destination fields are not the same. "

You do understand that message, right?

Votes

Translate

Translate

Report

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
New Here ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

Yes- I did not use column lists - or yes, I will consider using <cfqueryparam> .

Votes

Translate

Translate

Report

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
Mentor ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

LATEST
You should always use column lists, because if you don't, and the database table is modified by adding a new column, etc., all of your insert statements will fail. If you do use explicit column lists, then a column may be added to a table (if it is given a default value) without invalidating your INSERT statments. Of course, an insert would need to be modified to add the column if you need to insert values to that column with that insert statement, but it is just a good practice to explicitly list your columns so that you can match them up with the inserted values.

Phil

Votes

Translate

Translate

Report

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
Documentation