• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

How to create a form where the user enters there own query?

I would like to create a web form that will send a complete query to the action page.
In other words, I want the user to be able to design their query in this form, then submit it to the action page and it gets plugged into the CFQUERY tag of the action page.

This would be just a select query, not a insert, edit or delete.
Also, the field names and number of fields would need to be dynamic.  Which would be determined by the user's query from the form.

Is this possible?  If so, is there a code sample I can go by?
Thanks
0
g118481
Asked:
g118481
  • 3
  • 2
  • 2
  • +4
5 Solutions
 
mrichmonCommented:
wow VERY VERY VERY VERY DANGEROUS!!!!!!

I would not let the user actually create the query.

I would do something like :

Indicate the fields that you would like to view:

<checkbox>  Field1
<checkbox>  Field2
<checkbox>  Field3
<checkbox>  Field4
<checkbox>  Field5
<checkbox>  Field6


Then on the actionpage

<cfset fieldlist = "">
<cfif IsDefined("Form.field1")><cfset fieldlist = ListAppend(fieldlist, "Field1")></cfif>
<cfif IsDefined("Form.field2")><cfset fieldlist = ListAppend(fieldlist, "Field2")></cfif>
<cfif IsDefined("Form.field3")><cfset fieldlist = ListAppend(fieldlist, "Field3")></cfif>
<cfif IsDefined("Form.field4")><cfset fieldlist = ListAppend(fieldlist, "Field4")></cfif>
<cfif IsDefined("Form.field5")><cfset fieldlist = ListAppend(fieldlist, "Field5")></cfif>
<cfif IsDefined("Form.field6")><cfset fieldlist = ListAppend(fieldlist, "Field6")></cfif>

<cfif fieldlist NEQ "">
<cfquery name="UserQuery" datasource="mydsn">
   SELECT #fieldlist# FROM yourtable
</cfquery>

     
0
 
g118481Author Commented:
Ok, that sounds logical.
What about how to output the field list.  It may not always be the same?
0
 
mrichmonCommented:
You need to have some way of knowing what their choices will be.  This could be coded, it could be pulled from a query, it is up to you....
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
tim_csCommented:
What about if you just used mrichmon's way to let them choose the fields they want then use cfdump for the output of the query?
0
 
seangwCommented:
As stated above, a straight SQL statement is dangerous, but easily done (passing the SQL statement as a string, then executing the variable in the CFQUERY tag).

The method mentioned is great, and to return any results you get you can cfdump, or generate a formatted table with the column names.

To get the column names in a database you can always:

CFQUERY NAME=sqlQuery
SELECT * FROM tbl

then you can loop through the query

CFLOOP Query="sqlQuery"
    columnName = sqlQuery.Fieldname
    <cfoutput>Column Header: #columnName#</CFOUTPUT>

Then you can go ahead and display the contents of the query from this information
0
 
Seth_BienekCommented:

<cfquery> also returns a variable called "columnlist" that you can access to determine what columns were returned in the query, in a list format:

<cfquery name="myquery"...>
  SELECT * FROM mytable
</cfquery>

<table>
  <tr>
    <cfloop list="#myquery.columnlist#" index="i">
    <th>#i#</th>
    </cfloop>
  </tr>
<cfoutput query="myquery">
  <tr>
    <cfloop list="#myquery.columnlist#" index="i">
    <td>#evaluate("myquery.#i#")#</td>
    </cfloop>
  </tr>
</cfoutput>
</table>

Something like that.  

Regards,

Seth
0
 
cheekycjCommented:
how will you determine that it is a select vs an update/insert/delete?

Are you looking for basically search for the word insert/update/delete and don't run the query?

pay attention to words like drop/alter/create etc.

Is there a reason you would want to open your DB to anyone running a query?

I would look at the DB providing a level of security here.  Either provide a client interface to the users with a username that will just have read permission.

Otherwise use the web driven one with the same premise that the DB user account will just have read permission.

Again, one wrong query can cause big problems to a DB and even cause it to crash.

You may also want to consider putting time limits on execution of any query by the assigned user.

CJ

0
 
g118481Author Commented:
These are all good suggestions, and perhaps I should have further qualified the DB setup.
I have setup an account for this new web page to use in its query that only has read permission.
Also, this new web page will be used by my management team only.

Currently, I receive an email several times a week from different management individuals within my group, asking me to run these reports from our DB for them.  So, I thought it would be more efficient for them to run them their selves.  Not every request is identical, thus I have to manually adjust my query, run it, then export it to Excel and send it back to them.  This takes at least 4 or 5 hours of my time each week, so I thought it would be a time saver to let them run something for themselves.
0
 
Jerry_PangCommented:
i made this a few months ago.

<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=results.htm">
<CFCONTENT TYPE="text/html">
<cfset sqlstatement=form.sqlarea>
<cfquery datasource="#form.dns#" name="qry_results" username="#form.username#" password="#form.password#">
#sqlstatement#
</cfquery>
<cfdump var="#qry_results#" label="Results">

then moved this file to a directory with htapassword and access.
very dangerous indeed.

>>how will you determine that it is a select vs an update/insert/delete?
Datasource access in coldfusion, uncheck all query access except select statement
0
 
cheekycjCommented:
>> Datasource access in coldfusion, uncheck all query access except select statement

true, good point.

I have something similar to what Jerry_Pang and Seth posted, Its from one of my old postings on this site:

form.cfm:
<form action=query.cfm>
<textarea name=query>Enter Query Here</textarea>
<input name=submit type=submit>
</form>

query.cfm:

<CFSETTING ENABLECFOUTPUTONLY="yes">
<cfif IsDefined("FORM.query") AND LEN(TRIM(FORM.query)) GT 0>
<cfquery name="getData" datasource="blah">
#FORM.query#
</cfquery>
<cfheader name="content-disposition" value="inline;filename=Query_Results.xls">
<cfcontent type="application/octet-stream"> <!--- This forces a download rather than opening it in the browser --->
<cfoutput>
<table border="0">
     <tr>
     <tr>
     <cfloop index=ColumnName list=#getData.ColumnList#>
       <td>#ColumnName#</td>
     </cfloop>
     </tr>
     <cfloop query=getData>
     <tr>
     <cfloop index=ColumnName list=#getData.ColumnList#>
       <td><cfif LEN(TRIM(Evaluate("getData.#ColumnName#"))) GT 0>#Evaluate("getData.#ColumnName#")#<cfelse>&nbsp;</cfif></td>
     </cfloop>
     </tr>
     </cfloop>
</table>
</cfoutput>

Its dynamic based on columns returned by the query.

HTH,
CJ
0
 
g118481Author Commented:
Sorry, I have been out of the country.
The allocation above is fine with me.  Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now