I have a little problem in php. problem is that i want to search MySQL table by php coding. In php i want to use Drop down menu And a text field. I have two MySQL table name is category and products, now i use category entries in drop-down menu (by cat_name).and in the search text area i want write any product name selecting by cat_name from drop down menu and then click on search button. Then it will show me the result from product table in table format.
Can any one help me Thanks. i have tow category: Mobile and Laptop in categories table and i have many products name: Dell, Hp, Toshiba, Samsung, Iphone etc...in products table
1. categories
cat_id
cat_name
2. products
product_id
product_cat
product_name
product_price
I have php code. this working correct till populate cat_name from database in drop-down.
result.php
<?php
mysql_connect ("localhost", "root","") or die (mysql_error());
mysql_select_db ("ecomerce");
$sql = mysql_query('SELECT cat_name FROM categories ORDER BY cat_name');
$models = array();
while ($row = mysql_fetch_array($sql)){
$models[] = $row;
}
?>
<form action="search.php" method="post">
<select name="term">
<?php
foreach ($models as $model) {
?>
<option value="<?php echo $model['cat_name']?>"><?php echo $model['cat_name']?></option>
<?php
}
?>
</select>
<form >
<input type="text" class="form-control" placeholder="Search a Product">
<input type="submit" name="submit" value="Search" />
</form>
search.php
<?php
mysql_connect ("localhost", "root","") or die (mysql_error());
mysql_select_db ("ecomerce");
if(isset($_POST['term']) {
$term = $_POST['term'];
$query = "SELECT * FROM products WHERE product_cat = '".mysql_escape_string($term)."'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result) {
// display results
echo 'Product ID '.$row['product_id'];
echo 'product_title: '.$row['product_name'];
echo 'product_price: '.$row['product_price'];
}
}
?>
mysql_functions?!