douzhi1937 2013-08-21 05:18
浏览 37
已采纳

根据用户选项进行过滤并在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";}
?>
  • 写回答

1条回答 默认 最新

  • dtvfxzq3802 2013-08-21 05:27
    关注

    Change

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

    to

    $str = implode("and", $arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答