<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php
error_reporting(E_ALL); 
if (!isset($_POST['Submit'])) {
// form not submitted
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
search  
  <input type="text" name="search">
   <select size="1" name="dropdown">
    <option value="" selected>search By...</option>
    <option value="first">Company</option>
    <option value="last">Address</option>  
  </select>
  <input type="Submit" value="Submit" name="Submit"> 
</form>

<?php
}

else {

// Server Variables
$host = "localhost";
$user = "mdti";
$pass = "tnet";
$db = "ojt";

$search = empty($_POST['search'])? die ("ERROR: Enter search Criteria") : mysql_escape_string($_POST['search']);
$dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['dropdown']);


// Open Connection

$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");

//Select Database

mysql_select_db($db) or die ("Unable to connect to database");

//Create query

$query = "SELECT arCompanyname, arAddress FROM ar WHERE $dropdown like'$search'" or die (mysql_error());


$result = mysql_query($query) or die (mysql_error());

$num=mysql_numrows($result);

mysql_close($connect);

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$company=mysql_result($result,$i,"arCompanyname");
$address=mysql_result($result,$i,"arAddress");


echo "<br>Company: $company<br><br>Address: $address<hr><br>";

$i++;

}
}
?>
</body>
</html>

Good day! I'm just a newbie in php programming so I need somebody to help me with this one. I still cannot find what's wrong with this code. I created a search form with drop down menu to filter the results. But nothing happens when I do a searching. It's either it's just refreshes the page or display error criteria message. any answer will be appreciated! :) thanks in advance! Here is the code:

Hi, there are few errors here:

$query = "SELECT arCompanyname, arAddress FROM ar WHERE $dropdown like'$search'" or die (mysql_error());

$result = mysql_query($query) or die (mysql_error());
$num=mysql_numrows($result);

$query is a string, so do not include or die(mysql_error()) otherwise the mysql_query() will return FALSE. Then, mysql_numrows() does not exists, the correct function is mysql_num_rows(). If you still get errors then paste them here.

Last, but not least: since you're still learning, I strongy suggest you to avoid the MySQL API: it is deprecated and will be removed from PHP; learn MySQLi or PDO, for more information read this link:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.