0
<form action='main.php' method='POST'>
<select name="Category" class='listbox'>
<?php $cat = mysql_query("select cName from category");
while($drop = mysql_fetch_array($cat))
{
    echo '<option value="' . $drop['Category'] . '">' . $drop['cName'] . '</option>';
}
?>
<input type='text' name='search' class='namebox'>
<input type='submit' name='submit' value='Search' class='submitbox'></select>
</select>

i am trying to use this form to create a search engine from my database but just cant get the value from the drop down menu.

$submit = $_POST['submit'];

if($submit)
{
    $search = $_POST['search'];
    $catval = $_POST['Category'];
    echo $catval ;
    $searchval = mysql_query("select * from item where iname like '%$search%'and cId in (select cId from category where cName = '$catval')");
    while($info = mysql_fetch_array($searchval))
    {
        echo "Item Name: " . $info['iName'];
    }
}

so when i try to search using this method i get no results.

Omar
  • 3
  • 1
  • 1
    You should not query all fields (not use the * operator) and limit the result number. Don't process raw user input but unescape it or, even better, use prepared statements (with PDO) to make SQL injections impossible. SQL injections are a serious security problem. –  Jan 21 '15 at 13:23
  • 1
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 21 '15 at 13:28
  • Please note that I meant "escape", not "unescape" it. Unfortunately, my comment can no longer be edited. –  Jan 21 '15 at 13:29

2 Answers2

0

You have placed the selects closing tag wrong :) and I assume you remembered to close your form aswell?

<form action='main.php' method='POST'>
<select name="Category" class='listbox'>
<?php $cat = mysql_query("select cName, Category from category");
while($drop = mysql_fetch_array($cat))
{
    echo '<option value="' . $drop['Category'] . '">' . $drop['cName'] .'</option>';
}
?>
</select>
<input type='text' name='search' class='namebox'>
<input type='submit' name='submit' value='Search' class='submitbox'> 
</form>
Epodax
  • 1,828
  • 4
  • 27
  • 32
0
<form action='main.php' method='POST'>
<select name="Category" class='listbox'>
<?php $cat = mysql_query("select cName from category");
while($drop = mysql_fetch_array($cat))
{
    echo '<option value="' . $drop['Category'] . '">' . $drop['cName'] .         
'</option>';
}
?>
</select>
<input type='text' name='search' class='namebox'>
<input type='submit' name='submit' value='Search' class='submitbox'>
</form>

That should do the trick. Your </select> was in the wrong place.

In the meantime, you might want to look into PDO and bound values instead of using mysql() as it's depreciated (I.E. don't use it anymore) and insecure.

iamgory
  • 862
  • 1
  • 6
  • 10