Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch MySQL Database with Multiple Fields in a Form
    primarykey
    data
    text
    <p>I have created a form where the user can search the database, and the result depends on how the user fills out the form.<br> For example, say I have name, address, city, state, and zip field, and the user fills out name and city fields, the results reflect the input. When the form submits all records are displayed. for this I write this: </p> <pre><code>if(isset($_POST['submit'])) { $sql = mysql_query("SELECT * FROM table WHERE name LIKE '%" . $_POST['name'] . "%' OR address LIKE '%" . $_POST['address'] . "%' OR city LIKE '%" . $_POST['city'] . "%' OR state LIKE '%" . $_POST['state'] . "%' OR zip LIKE '%" . $_POST['zip'] . "%'"); } &lt;form method="post" action="&lt;?php $_SERVER['PHP_SELF']; ?&gt;"&gt; &lt;tr&gt; &lt;td&gt;Name:&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="name" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Address:&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="address" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;City:&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="city" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;State:&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="state" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Zip:&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="zip" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&lt;input type="submit" name="submit" value="Search" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/form&gt; &lt;/table&gt; &lt;?php if(isset($_POST['submit'])) { while($row = mysql_fetch_array($sql)) { echo $row['name'] . "&lt;br /&gt;"; } } ?&gt; </code></pre> <p>But in this case a user may leave a field blank.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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