2016-06-17 10 views
0

私が理解していないことは、show_everything()関数は機能しますが、show_search()関数は機能しないことです。私は$ searchをエコーし​​ようとしましたが、それは正しいです、それは何でしょうか?あなたはまた、より良い作業のコードを持っている場合、それはあなたの検索クエリが完全一致を探している私はeコマースのWebサイトでphpとsqlを検索しようとしていますが、なぜこのコードは機能していませんか?

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 
     } 
     } 
    ?> 
+0

SQLの文字列値の引用符がありません。 –

+0

あなたは直面しているエラーと '='の代わりに 'like'を使用します。 –

+0

ok thanks Jonはエラーでした。 – valbuxvb

答えて

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 
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); 
    } 
関連する問題