0

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'];


    }
}

?>
1
  • People are seriously still using the mysql_ functions?! Commented Apr 26, 2015 at 19:54

1 Answer 1

1

config.php

<?php
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_USER', 'DB_Username'); 
DEFINE ('DB_PASSWORD', 'DB_Password');
DEFINE ('DB_NAME', 'Database_Table_Name'); 
DEFINE ('DBCONN', 'Path/to/dbconnect.php');
?>

dbconnect.php

<?php
$dbconn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
IF (!$dbconn) {
    die('Could not connect: ' . mysql_error());
}
IF (!mysql_select_db (DB_NAME)) { die('Could not select table: ' . mysql_error()); }
?>

functions.php

<?php
// Categories
Function Categories($mysql = true) { 

    $result = array();

    IF ($mysql == true) { require(DBCONN); }ELSE{ global $dbconn; } // db connection
    // query cats to array
    $sql = mysql_query("SELECT cat_name FROM categories ORDER BY cat_name");
    while ($row = mysql_fetch_array($sql)){
        $result[] = stripslashes($row['cat_name']);
    }
    mysql_free_result($sql);
    IF ($mysql == true) { mysql_close($dbconn); } // close connection

    return $result;

}

// Category dropdown
Function ddform_Categories($cats, $post = NULL) { 

    $result = "";

    // set default
    IF ((empty($post)) || ($post == "xx")) { 
        $result .="<option selected value=\"xx\">Choose Category</option>"; 
    }ELSE{ 
        $result .="<option value=\"xx\">Choose Category</option>"; 
    }

    foreach ($cats as $category) {
        IF ($post == $category) { 
            $result .="<option selected value=\"".$category."\">".$category."</option>";
        }ELSE{ 
            $result .="<option value=\"".$category."\">".$category."</option>";
        }
    }

    return $result;

}

// MySQL Search
Function CategorySearch($mysql = true, $cats, $post) {

    $result = "";
    $rows = "";

    IF ($mysql == true) { require(DBCONN); }ELSE{ global $dbconn; } // db connection
    $term = mysql_escape_string($post);
    $query = "SELECT * FROM products WHERE product_cat = '$term'";
    $sql = mysql_query($query);
    IF (mysql_num_rows($sql)) {
        while($r = mysql_fetch_array($sql) {
            $rows .= "<tr><td>".$r['product_name']."</td><td>$".number_format($r['product_price'])."</td></tr>";
        }
        mysql_free_result($sql);

    }ELSE{
        $result = "<p>No Results</p>";
        #$result .= "<p>SQL:\n".$query ."</p>"; // debug
    }

    IF ($mysql == true) { mysql_close($dbconn); } // close connection

    IF (!empty($rows)) {
        $result = "<table>".$rows."</table>";
    }

    return $result;

}
?>

Search.php

<?php
Require('config.php');
Include('functions.php')

$cats = Categories();
$post = "xx"; // default post value

IF (isset($_POST['Search'])) {

    $post = htmlspecialchars(strip_tags(trim($_POST['term'])));

    // build query
    IF ( (!empty($post)) && (in_array($post, $cats)) ) {
        $result = CategorySearch(true, $post);
    }ELSE{
        $result = "<p>Search option (".$post.") was invalid.</p>";
    }

    echo($result);

}
?>

<form action="search.php" name="search" method="post">
  <select name="term"><?php echo(ddform_Categories($cats, $post))); ?></select>
    <input type="text" class="form-control" placeholder="Search a Product">
    <input type="submit" name="Search" value="Search" />
</form>
Sign up to request clarification or add additional context in comments.

6 Comments

i am getting this error.......Parse error: parse error in C:\xampp\htdocs\link\drop\Search.php on line
I need the full error message including the line number.
Parse error: parse error in C:\xampp\htdocs\link\drop\Search.php on line 5
Line 5 is : $cats = Categories(); Make sure the dbconnect.php file is correct and you updated the config with your login, host, db name credentials.
Update the Categories function $sql line as follows: $sql = mysql_query("SELECT cat_name FROM categories ORDER BY cat_name");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.