Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a table search function with multiple variable PHP MySQL
    primarykey
    data
    text
    <p>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 <code>POST</code> 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 <code>$search</code> 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.</p> <pre><code>//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"); } </code></pre> <p>Added the form code showing the variables.</p> <pre><code>echo '&lt;form ENCTYPE=multipart/form-data form name=benchsearch action=benchsearchresults.php method=post&gt;'; //search selection echo '&lt;table&gt;'; echo '&lt;tr&gt;&lt;td&gt;Standard Search &lt;/td&gt;'; echo "&lt;td&gt;&lt;input type=text maxlength=30 name=search /&gt;&lt;/td&gt;"; echo '&lt;tr&gt;&lt;td&gt;Search Status of Equipment &lt;/td&gt;'; echo "&lt;td&gt;&lt;select name=status&gt;"; echo "&lt;option&gt; &lt;/option&gt;"; echo "&lt;option&gt; SERVICEABLE &lt;/option&gt;"; echo "&lt;option&gt; UNSERVICEABLE &lt;/option&gt;"; echo "&lt;option&gt; BEYOND ECONOMICAL REPAIR &lt;/option&gt;&lt;/select&gt;&lt;/td&gt;"; echo "&lt;tr&gt;&lt;td&gt;Search Type of Repair :&lt;/td&gt;"; echo "&lt;tr&gt;&lt;td&gt;Repair &lt;/td&gt;"; echo "&lt;td&gt;&lt;input type=checkbox name=repair value=rep /&gt;&lt;/td&gt;"; echo "&lt;tr&gt;&lt;td&gt;Updrade/Modification &lt;/td&gt;"; echo "&lt;td&gt;&lt;input type=checkbox name=upgrade value=um /&gt;&lt;/td&gt;"; echo "&lt;tr&gt;&lt;td&gt;Preventitive Maintenance Check &lt;/td&gt;"; echo "&lt;td&gt;&lt;input type=checkbox name=pm value=pm /&gt;&lt;/td&gt;"; echo "&lt;tr&gt;&lt;td&gt;No Fault Found &lt;/td&gt;"; echo "&lt;td&gt;&lt;input type=checkbox name=nofault value=nf /&gt;&lt;/td&gt;"; echo "&lt;/table&gt;"; echo '&lt;center&gt;&lt;input type="submit" class="button" id="submit" value="Search"/&gt; &lt;/center&gt;'; echo "&lt;/form&gt;"; </code></pre> <p>My tables are as follows</p> <p>Table aecm, aicm etc Columns - part, serial, mac, ip, status</p> <p>Table aecmhis, aicmhis etc Columns - serial, repair, upgrade, pm, nofault</p> <p>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 <code>yes</code> in them. Just looking for the best way to go about this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload