I am trying to create a where clause that loops through my foreach loop. The idea is to have a search with four possible values depending on which values the user fills in.
,<?php
$fname = $_GET['fname'];
$lname = $_GET['lname'];
$nature =$_GET['nature'];
$ticket_id = $_GET['ticket_id'];
$nbsp = " ";
$fields = array('fname', 'lname', 'ticket_id', 'nature');
$whereClause = "WHERE" ;
foreach ($fields as $field)
{
if(isset($_GET[$field]) and !empty($_GET[$field]))
{
$whereClause .= $field . " LIKE '%" . $_GET[$field] . "%' OR ";
}
// else { return "no results found";
// }
}
substr($whereClause, 0, -4);
$query = ("SELECT fname, lname, ticket_id, nature FROM trouble_ticket = '$whereClause' ");
$result = mysql_query($query)or die($query."<br/><br/>".mysql_error());
while($row = mysql_fetch_array( $result )) // calls the information from the db places it in a table
{
echo "<tr>";
echo "<td bgcolor= white>".$row['fname']. " ".$nbsp. " ".$row['lname']. " </td>";
echo "<td bgcolor= white>".$row['lname']. " </td>";
echo "<td bgcolor= white>".$row['ticket_id']. "</td> ";
echo "<td bgcolor= white>".$row['nature']. " </td>";
echo"</tr>";
}
echo "</table>";
mysql_free_result($result);
mysql_close($conn);
?> '
The error I am getting is the following: SELECT fname, lname, ticket_id, nature FROM trouble_ticket = 'WHERElname LIKE '%paulson%' OR '
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''WHERElname LIKE '%paulson%' OR '' at line 1
I thought I had the right syntax for the where clause but I guess I don't, help with this would be greatly appreciated. Thanks