Hi all...
I have a Form (shown below) that runs a MySQL Query (also shown below) that is supposed to return results based upon whether a field in the form is populated with a string or not. If it's not populated, it should act as a null, so it is not included in the query.
The form :
<table border=0 cellpadding=0 cellspacing=0 style=\"border-collapse: collapse\" width=75% class=table>
<tr class=tr2>
<td width=100% align=center colspan=6 class=td2b>Flight Search Table</td>
</tr>
<tr class=tr1>
<td width=16% align=center class=td1><b>Flt #:</b></td>
<td width=16% align=center class=td1><b>Hub:</b></td>
<td width=18% align=center class=td1><b>Dep ICAO:</b></td>
<td width=18% align=center class=td1><b>ETE (hrs):</b></td>
<td width=16% align=center class=td1><b>Arr ICAO</b></td>
<td width=16% align=center class=td1><b>Aircraft:</b></td>
</tr>
<tr bgcolor=gray>
<td width=100% colspan=6 class=td1></td>
</tr>
<tr bgcolor=#F1F1F1>
<td width=16% align=center class=td1><input type=text name=flt value=\"$_POST[flt]\" size=5 class=form></td>
<td width=16% align=center class=td1><input type=text name=hub value=\"$_POST[hub]\" size=5 class=form></td>
<td width=18% align=center class=td1><input type=text name=dicao value=\"$_POST[dicao]\" size=5 class=form></td>
<td width=18% align=center class=td1><input type=text name=ete value=\"$_POST[ete]\" size=5 class=form></td>
<td width=16% align=center class=td1><input type=text name=aicao value=\"$_POST[aicao]\" size=5 class=form></td>
<td width=16% align=center class=td1><input type=text name=ac value=\"$_POST[ac]\" size=5 class=form></td>
</tr>
<tr>
<td width=20% align=center colspan=6 class=td1> </td>
</tr>
<tr>
<td width=20% align=center colspan=6 class=td1><input type=submit name=submit value=Search class=submit></td>
</tr>
</table>
And the MySQL Query :
$flt = "SELECT * FROM flight";
if( !empty($_POST['flt']) || !empty($_POST['hub']) || !empty($_POST['dicao']) || !empty($_POST['ete']) || !empty($_POST['aicao']) || !empty($_POST['ac']) ) $flt .= " WHERE ";
if( !empty($_POST['flt']) && $_POST['flt'] != 'nothing' ) $flt .= " flight_num LIKE '%" . $_POST['flt'] . "%' ";
if( !empty($_POST['hub']) && $_POST['hub'] != 'nothing' ) $flt .= " flight_hub LIKE '%" . $_POST['hub'] . "%' ";
if( !empty($_POST['dicao']) && $_POST['dicao'] != 'nothing' ) $flt .= " flight_icao_dep LIKE '%" . $_POST['dicao'] . "%' ";
if( !empty($_POST['ete']) && $_POST['ete'] != 'nothing' ) $flt .= " flight_ete LIKE '%" . $_POST['ete'] . "%' ";
if( !empty($_POST['aicao']) && $_POST['aicao'] != 'nothing' ) $flt .= " flight_icao_arr LIKE '%" . $_POST['aicao'] . "%' ";
if( !empty($_POST['ac']) && $_POST['ac'] != 'nothing' ) $flt .= " flight_aircraft LIKE '%" . $_POST['ac'] . "%' ";
$flt_result = @mysql_query($flt, $connect) or die(mysql_error());
The problem I'm running into is that once a user enters a single string into one of the fields, and only searches with one string / field (such as Flt #), the MySQL query returns a value.
However, if a user attempts to search using more than one string (like Flt # and Dep ICAO for example), I get the following error message (in this case I searched for a Flt # and the Dep ICAO string KSEA) :
You have an error in your SQL syntax near 'flight_hub LIKE '%KSEA%' ' at line 1
I'm not exactly sure what's wrong here. I'm pretty much new to some of the more advanced PHP functions and the like, and this particular search query string I got from the following thread here on the forums : http://www.phpbuilder.com/board/showthread.php?s=&threadid=10265612&highlight=mysql+search+wildcard . I've followed the instructions there and have gotten as far as the problem above.
Can anyone shed some light on where I've gone wrong, or where the problem is in my code? Thanks for any help...