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

SQL query and CFOUTPUT question

New Here ,
May 04, 2010 May 04, 2010

Copy link to clipboard

Copied

Hi,

I'm trying to compare two tables in a database.  I need to output a few fields from one table, compare one field between the two tables.  Users are selecting the tables.  I have written an SQL query which does what I need:

<cfquery name="getitems" datasource="snapshot">
select #endtable#.field9, #endtable#.field12, #endtable#.field4, #endtable#.field13, #endtable#.field2, #endtable#.field3, #starttable#.field3
from #endtable#, #starttable#
where #endtable#.field5='#form.field5#'
and #starttable#.field2=#endtable#.field2
</cfquery>

Problem is on output.  You can see I am selecting field3 from two tables.  I also want to output both field3 values.

<cfoutput query="getitems">

#field3#

</cfoutput>

This gives me the value which was selected as #endtable#.field3.

How can I output the other -- #starttable#.field3?

As noted, the table names are variables being passed into the query...  Thanks!

Peter

TOPICS
Advanced techniques

Views

782

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

correct answers 1 Correct answer

Valorous Hero , May 04, 2010 May 04, 2010

How can I output the other -- #starttable#.field3?

Use an alias to give the fields different names ie table.column AS SomeName. Otherwise, cfquery may not know which field you mean when you say "field3".

select #endtable#.field9, #endtable#.field12,

Be careful using this type of sql. Depending on the source of your variables, this can pose a sql injection risk.

Votes

Translate

Translate
Valorous Hero ,
May 04, 2010 May 04, 2010

Copy link to clipboard

Copied

How can I output the other -- #starttable#.field3?

Use an alias to give the fields different names ie table.column AS SomeName. Otherwise, cfquery may not know which field you mean when you say "field3".

select #endtable#.field9, #endtable#.field12,

Be careful using this type of sql. Depending on the source of your variables, this can pose a sql injection risk.

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 ,
May 04, 2010 May 04, 2010

Copy link to clipboard

Copied

Thank you, that worked beautifully!  Another trick learned..

Also, thanks for the sql injection warning -- I am cleaning the input before passing it into the query.  Much appreciated!

Peter

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 ,
May 04, 2010 May 04, 2010

Copy link to clipboard

Copied

Regarding:

I am cleaning the input before passing it into the query

Even this one?

'#form.field5#'

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 ,
May 05, 2010 May 05, 2010

Copy link to clipboard

Copied

Hi,

The #form.field5# comes via a select list....no user input of data.

I suppose it is still possible to be compromised.  I'll fix that.  Thanks!

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
Valorous Hero ,
May 05, 2010 May 05, 2010

Copy link to clipboard

Copied

LATEST

The #form.field5# comes via a select list....no user input

of data....

Someone could easily construct a fake http post, with malicious sql in the "form" fields, and submit it to your action page. That is why it is vulnerable.

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