Solved

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

Posted on 2004-08-02
13
322 Views
Last Modified: 2013-12-24
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
Comment
Question by:g118481
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
13 Comments
 
LVL 35

Accepted Solution

by:
mrichmon earned 20 total points
ID: 11694931
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
 
LVL 1

Author Comment

by:g118481
ID: 11695882
Ok, that sounds logical.
What about how to output the field list.  It may not always be the same?
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 11695962
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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
LVL 15

Expert Comment

by:tim_cs
ID: 11696324
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
 
LVL 1

Assisted Solution

by:seangw
seangw earned 20 total points
ID: 11696432
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
 
LVL 5

Assisted Solution

by:Seth_Bienek
Seth_Bienek earned 20 total points
ID: 11696973

<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
 
LVL 19

Expert Comment

by:cheekycj
ID: 11698116
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
 
LVL 1

Author Comment

by:g118481
ID: 11699008
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
 
LVL 9

Assisted Solution

by:Jerry_Pang
Jerry_Pang earned 20 total points
ID: 11700266
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
 
LVL 19

Assisted Solution

by:cheekycj
cheekycj earned 20 total points
ID: 11704201
>> 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
 
LVL 1

Author Comment

by:g118481
ID: 12005893
Sorry, I have been out of the country.
The allocation above is fine with me.  Thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
What You Need to Know when Searching for a Webhost Provider
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question