使用下拉菜单和文本字段通过Php过滤我的SQL

时间:2015-04-26 18:20:39

标签: php html mysql

我在php中有一点问题。 问题是我想通过php编码搜索MySQL表。 在PHP我想使用下拉菜单和文本字段。 我有两个MySQL表名是类别和产品,现在我使用类别条目 在下拉菜单中(通过cat_name)。和在搜索文本区域中 我想从下拉菜单中选择cat_name选择的任何产品名称,然后单击搜索按钮。然后它会以表格格式显示产品表的结果。

任何人都可以帮助我 谢谢。  我有两类:移动和笔记本电脑类别表和  我有很多产品名称:戴尔,惠普,东芝,三星,Iphone等......在产品表中

1。类

CAT_ID
cat_name

2。产品

product_id 
product_cat 
product_name 
product_price

我有PHP代码。这工作正确,直到从下拉列表中的数据库填充cat_name。

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 个答案:

答案 0 :(得分: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>