I would like to have my users type in a term and display results from two different tables. I tried using union but i'm getting errors can you help please?
<?php
mysql_connect ("localhost", "","") or die (mysql_error());
mysql_select_db ("");
if(isset($_POST['submit']) && !empty($_POST['submit'])){
$result = ""; //USED LATER
/*$term = $_POST['term']; THIS WORKS BUT FOR SECURITY ISSUES USE:*/
$term = mysql_real_escape_string($_POST['term']);//AVOID MYSQL INJECTION
$sql = mysql_query("SELECT * FROM `Cater Gid` where Cater like '%$term") UNION ("SELECT * FROM `Cater Food` where Cater like '%$term") ;
if (mysql_num_rows($sql) <= 0) {
// no results
//echo 'No results found.'; BETTER ECHO LATER
$error = "No result found";
} else if ($term ="") {
$error = "No name entered!";
} else {
$result .= "<table border='1'>";
$result .="<tr><td>Cater</td><td>Part</td><td>Gids</td></tr>";
while ($row = mysql_fetch_array($sql)){
$result .= '<tr>';
$result .= '<td>'.$row['Cater'].'</td>';
$result .= '<td>'.$row['Part'].'</td>';
$result .= '<td>'.$row['Gid'].'</td>';
$result .= '</tr>';
}
$result .= "</table>";
$result .= "<table border='1'>";
$result .="<tr><td>Cater</td><td>Dish</td><td>Gids</td></tr>";
while ($row = mysql_fetch_array($sql)){
$result .= '<tr>';
$result .= '<td>'.$row['Cater'].'</td>';
$result .= '<td>'.$row['Dish'].'</td>';
$result .= '<td>'.$row['Gid'].'</td>';
$result .= '</tr>';
}
$result .= "</table>";
}
mysql_close();
}
?>
<html>
<head>
<title>FIND YOUR GID</title>
</head>
<body color="" bgcolor="#634673" text="white"><font size="7"></font>
<center><font size="7"> </font><br><br><br><br>
<h1> <center>MORE DATA COMING SOON! </center></h1>
<h2> <center>ENTER NAME OF CATER! </center></h2>
<form action="caterfood.php" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<div name="Results">
<?php
if(isset($error)){echo $error;}
if(isset($result)){echo $result;}
?>
</div>
</body>
</html>
<script type="text/javascript">
var arrRequiredFields = [ "term" ];
window.onload = function() {
document.forms[0].onsubmit = function() {
for (var i = 0; i < arrRequiredFields.length; i++) {
var field = document.forms[0].elements[arrRequiredFields[i]];
if (field && field.value.length == 0) {
alert("Missing Name of Food");
field.focus();
return false;
}
}
return true;
};
};
</script>