Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP, MySQL, Array from HTML Form in Search Query?
    text
    copied!<p>I have a search engine with an html form that sends data to a PHP script the queries a MySQL database. In the html form, I have a option that allows multiple selections. Thus far, I have put square brackets after the name in the HTML in order to make it an array. But I think there is a problem in the PHP because results are not correct.</p> <p>HTML</p> <pre><code>&lt;select multiple="multiple" name='category[]'&gt; &lt;option&gt;Literature&lt;/option&gt; &lt;option&gt;History&lt;/option&gt; &lt;option&gt;Science&lt;/option&gt; &lt;option&gt;Fine Arts&lt;/option&gt; &lt;option&gt;Trash&lt;/option&gt; &lt;option&gt;Mythology&lt;/option&gt; &lt;option&gt;Phylosophy&lt;/option&gt; &lt;option&gt;Social Science&lt;/option&gt; &lt;option&gt;Religion&lt;/option&gt; &lt;option&gt;Geography&lt;/option&gt; &lt;/select&gt; </code></pre> <p>search.php</p> <pre><code>$button = $_GET ['submit']; $search = $_GET ['search']; } if(strlen($search)&lt;=1) echo "Search term too short"; else{ echo "You searched for &lt;b&gt;&lt;em&gt;$search&lt;/em&gt;&lt;/b&gt; and "; mysql_connect("fake","fake","fake"); mysql_select_db("quinterestdb");} mysql_real_escape_string($search); $search_exploded = explode (" ", $search); foreach($search_exploded as $search_each) { $x++; if($x==1) $construct .="Answer LIKE '%$search_each%'"; else $construct .="AND Answer LIKE '%$search_each%'"; } $cat = $_GET ['category']; $comma_separated = implode("','", $cat); $constructs ="SELECT * FROM tossups WHERE $construct AND Category IN('$comma_separated')"; $run = mysql_query($constructs); </code></pre> <p>When I use the search engine, the script runs just fine, but there are still results that have categories that were not selected. Any idea?</p>
 

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