我试图通过过滤为用户选择的选项从数据库中获取结果。用户可以根据酒名或酒厂或两者搜索酒店。我编写了以下命令,它应该根据用户的选择附加特定的 sql 命令。但它给了我服务器错误。我认为我的阵列或任何东西有问题。请有人帮我解决这个问题。
<?php
$searchResult = "";
$WineName = preg_replace('#[^a-z]#i','',$_POST['WineName']);
$winery = preg_replace('#[^a-z]#i','',$_POST['winery']);
$arr = array();
if(!empty($_POST['WineName']))
$arr[] = "wine_name LIKE '%$WineName'";
if(!empty($_POST['winery']))
$arr[] = "winery_name LIKE '%$winery";
$str = impode("and", $arr);
if(!empty($str)) $str = "and ".$str;
include_once("connect.php");//requesting to open the database
$query = mysql_query("select *from WineSearchView where 1 $str") or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$searchResult .= "$count results for $WineName";
while($row = mysql_fetch_array($query)){
$wName = $row["wine_name"];
$variety = $row["variety"];
$year = $row["year"];
$wineryName = $row["winery_name"];
$region = $row["region_name"];
$cost = $row["cost"];
$stock = $row["on_hand"];
$ordered = $row["qty"];
$searchResult .= "Wine Name: $wName, V: $variety, Y: $year, WN: $winery_name, R: $region, C: $cost, S: $stock, O: $ordered<br />";
}//close while
}
else { $searchResults = "0 results for $WineName $sqlCommand";}
?>