I am trying to create a search function with multiple variables. Basically I have a standard style form, and each element has a name. Those names on submit are retrieved via POST
then the search query is conducted. I have posted my code and I know I have probably gone about this a terrible way but I am looking for direction on how to make it suitable. Still very new to PHP so advice on better ways to structure this that I can then go and learn/implement would be appeciated. Currently it does search via the first variable $search
but I'm not sure how to implement the rest. The user should be able to input 1 or multiple variables then the search is conducted. At this point it is only searching one table and I would like it to search multiple tables long term, all the columns in the different tables have the same name, just different data. Any guidance appreciated here. Cheers.
//define variables to be used
$table = 'aecm';
$search = @$_POST['search'];
$status = @$_POST['status'];
$repair = @$_POST['repair'];
$upgrade = @$_POST['um'];
$pm = @$_POST['pm'];
$nofault = @$_POST['nf'];
//Connect to database
include '/../connect.php';
//conduct a search
$result = mysql_query("SELECT * FROM $table
WHERE (part LIKE '%$search%')
OR
(serial LIKE '%$search%')
OR
(mac LIKE '%$search%')
OR
(ip LIKE '%$search%')
OR
(status LIKE '%$search%')
ORDER BY part ASC");
if (!$result) {
die("Query to show fields from table failed");
}
Added the form code showing the variables.
echo '<form ENCTYPE=multipart/form-data form name=benchsearch
action=benchsearchresults.php method=post>';
//search selection
echo '<table>';
echo '<tr><td>Standard Search </td>';
echo "<td><input type=text maxlength=30 name=search /></td>";
echo '<tr><td>Search Status of Equipment </td>';
echo "<td><select name=status>";
echo "<option> </option>";
echo "<option> SERVICEABLE </option>";
echo "<option> UNSERVICEABLE </option>";
echo "<option> BEYOND ECONOMICAL REPAIR </option></select></td>";
echo "<tr><td>Search Type of Repair :</td>";
echo "<tr><td>Repair </td>";
echo "<td><input type=checkbox name=repair value=rep /></td>";
echo "<tr><td>Updrade/Modification </td>";
echo "<td><input type=checkbox name=upgrade value=um /></td>";
echo "<tr><td>Preventitive Maintenance Check </td>";
echo "<td><input type=checkbox name=pm value=pm /></td>";
echo "<tr><td>No Fault Found </td>";
echo "<td><input type=checkbox name=nofault value=nf /></td>";
echo "</table>";
echo '<center><input type="submit" class="button" id="submit" value="Search"/> </center>';
echo "</form>";
My tables are as follows
Table aecm, aicm etc Columns - part, serial, mac, ip, status
Table aecmhis, aicmhis etc Columns - serial, repair, upgrade, pm, nofault
I want to use a form as above (or similair) to conduct a search and show results from all tables. The serial number is common between aecm-aecmhis and aicm-aicmhis etc. Status is only 1 of 3 options and the repair, upgrade, pm, nofault columns are either empty or have yes
in them. Just looking for the best way to go about this.