根据用户选项进行过滤并在php中附加sql命令

Im trying to get the result from the database by filtering as the user's selected options. User can search the wine store according to the wine name or winery or both. I wrote the following command and it should attach the specific sql command depending on user's selection. but it is giving me server error. I think there is a problem with my array or anything. Please someone help me with this.

<?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";}
?>

Change

$str = impode("and", $arr);

to

$str = implode("and", $arr);