我正在尝试使用php和sql搜索电子商务网站,为什么这段代码不起作用?

时间:2016-06-17 09:32:49

标签: php html sql

我不明白的是show_everything()函数有效,但show_search()没有。我试图回应$ search,这是正确的,它会是什么?如果你也有一个更好的工作代码,那将是伟大的:)感谢帮助

if(isset($_POST['search'])){
    $search = $_POST['search'];
    show_search($search);
  }else{
    show_everything();
  }

  function show_search($search){
    $result = mysql_query("SELECT * FROM products WHERE nome = $search OR codice = $search OR descrizione = $search");
    show($result);
  }


  function show_everything(){
     $result = mysql_query("SELECT * FROM products");
     show($result);
  }


function show($result){
  while($row = MYSQL_FETCH_ROW($result)){
     ?>
         <div id="longinfo" style="display:inline-block;" class="col-xs-12 col-sm-6 col-md-6 col-lg-4 container">
           <div id="ctnpreview">
             <div class="row" id="shortinfo">
               <div id="imagepreview" style="float:left;">
                 <div class="imagebackground" style="margin-right:auto; margin-left:auto;">
                   <img class="helper" src="images/<?php echo "$row[0]/$row[0].jpg"; ?>">
                   <span class="helper"></span>
                 </div>
               </div>
               <div id="pricenavailable" style="float:left; margin-top:14px;">
                 <div class="panel panel-default">
                   <div id="price" class="panel-heading"><h3>&#8364 <?php echo $row[2]; ?></h3></div>
                   <div class="panel-body"><?php
                     if($row[4]>=5){
                       echo '<div class="label label-success">Disponibilità: '.$row[4].'</div>';
                     }elseif ($row[4]<5 && $row[4]>0) {
                       echo '<div class="label label-warning">Disponibilità: '.$row[4].'</div>';
                     }else{
                       echo '<div class="label label-danger">Disponibilità: '.$row[4].'</div>';
                     }
                   ?></div>
                 </div>
               </div>
             </div>
             <div id="addcart" class="btn btn-primary">
               <i class="fa fa-cart-plus" aria-hidden="true"></i>  Aggiungi al carrello
             </div>
             <div class="panel panel-info">
               <div class="panel-heading">
                 <strong><?php echo $row[1]; ?></strong>
               </div>
               <div class="panel-body">
                 <?php echo $row[5]; ?>
                 <hr>
                 <?php
                   foreach($row[3] as $char){
                     echo $char;
                   }
                 ?>
                 <a href=""><div class="btn btn-info">Visualizza Prodotto</div></a>
               </div>
             </div>
           </div>
         </div>
         <style>
           #ctnpreview{
             margin-left:auto;
             margin-right:auto;
             text-align: center;
             border:1px solid rgb(180, 183, 185);
             border-radius: 5px;
             background-color: rgb(247, 247, 250);
             padding-bottom:10px;
           }
           #longinfo{
             margin-bottom: 10px;
           }
           #shortinfo{
             padding-left:20px;
           }
           #addcart{
             margin-bottom:20px;
           }
         </style>
       <?php
         }
      }
    ?>

2 个答案:

答案 0 :(得分:0)

"SELECT * FROM products WHERE nome = '".$search."' OR codice = '".$search."' OR descrizione = '".$search."'" 

// this query will match the exact string from the search box


"SELECT * FROM products WHERE nome LIKE '%".$search."%' OR codice LIKE '%".$search."%' OR descrizione LIKE '%".$search."%'"

// this query will show the matching string from the search box

答案 1 :(得分:0)

您的搜索查询正在搜索完全匹配。您需要的是包含搜索词的任何内容。对函数使用以下定义。

  function show_search($search){
    $result = mysql_query("SELECT * FROM products WHERE nome LIKE '%".$search."%' OR codice LIKE '%".$search."%' OR descrizione LIKE '%".$search."%'");
    show($result);
  }