How to compare user inputs with 2 different tables

Viewed 76

I have a form which takes in the values of First Name, Last Name and DOB. I have two tables: TableA and TableB. Each table may contain information of the person name that is entered in the form. What I would like to do, in ColdFusion, write a query where I take the user inputs and compare which Table contains the info. Once I find which table contains the user info, display particular columns that pertain in that table.

Below is the form:

<Form name="ERHospital" Method="Post" class="a">

  <div align="center" style="margin-top:10pt; margin-bottom:10pt">

    <table Border="0" width="100%" align="center" cellpadding=0 cellspacing=0 >
        <tr>
            <td class="m" id="fName">First Name&nbsp;</td>
            <td>
                <input class="a" id="FirstName" name="FirstName" value="<cfoutput></cfoutput>" size="15"> 
            </td>
            <td class="m" id="lName">Last Name&nbsp;</td>
            <td>
                <input class="a" id="LastName" name="LastName" value="<cfoutput></cfoutput>" size="15"> 
            </td>

            <td class="m" id="DOB">
                DOB<br />
                <font color="##CC0000" size="1" style="italic">(mm/dd/yyyy)</font>
            </td>
            <td>
                <input type="text" id="BirthDt" name ="BirthDt" id="BirthDt" maxlength="10" size="10">
            </td>
        </tr>
        <tr>
            <!---<td class="m" id="Hospital">Hospital</td>
            <td>
                <select name="hosp" id="hosp"></select>
            </td>--->
            <!---<td class="m" colspan="2"></td>
            <td class="m">
                <input type="button" value="Submit" onClick="SubmitMe(); checkFields();" name="SubmitEmpBtn" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; font-weight:bold">
            </td>
            <td align="left">
                <input type="button" name="Clear"  value="Clear" onClick="ClearForm()" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; font-weight:bold"> 
            </td>  
        </tr>--->
    </table>
    <div style="text-align:center; margin-top:15pt"><input type="button" value="Submit" onClick="SubmitMe(); checkFields();" name="SubmitEmpBtn" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; font-weight:bold"> <input type="button" name="Clear"  value="Clear" onClick="ClearForm()" style="font-size:12px; font-family:Arial, Helvetica, sans-serif; font-weight:bold"> </div>
   <!---<table align="center">
                <TR>
                  <TD align="center"><input type="button" value=" Submit " onClick="SubmitMe()" name="SubmitEmpBtn" style="font-size:14px; font-family:Arial, Helvetica, sans-serif"> <input type="button" name="Clear"  value=" Clear " onClick="ClearForm()" style="font-size:14px; font-family:Arial, Helvetica, sans-serif"> </td>   
                </tr>
            </table>

  </table>--->

   </div>
</FORM>
1 Answers

If the user is guaranteed to be in only one table, then you can use a UNION ALL like the following:

<cfquery name="myquery" datasourse="#myDSN#">
    SELECT <userField1>, <userField2>
    FROM Table1
    WHERE 
        FirstName = <cfqueryparam value="#form.firstname#" cfsqltype="cf_sql_varchar">
        AND
        LastName = <cfqueryparam value="#form.lastname#" cfsqltype="cf_sql_varchar">

    UNION ALL

    SELECT <userField1>,<userField2>
    FROM Table2
    WHERE 
        FirstName = <cfqueryparam value="#form.firstname#" cfsqltype="cf_sql_varchar">
        AND
        LastName = <cfqueryparam value="#form.lastname#" cfsqltype="cf_sql_varchar">
</cfquery>

If the user is only in one table, you'll only get the results for your requested user.

EDIT: I would also add that I am not a big fan of using form, url or any other external variable in a sql query without making sure they have been sanitized. But that is a WHOLE different type of discussion. :-)

Related