2016-04-21 15 views
0

Also habe ich an einem Programm gearbeitet, das Informationen aus einer MySql-Datenbank ergreift und dann nimmt es und legt es in eine Tabelle und es hat auch die Fähigkeit, es zu filtern, wenn ich es einlege ein einzelner Parameter, es funktioniert gut, aber es nicht mit zwei oder mehr Parametern arbeitet Hier ist, was ich habe, schafft Dieser Teil der MySQL-Abfrage-AnweisungFilter-Funktion funktioniert nur mit einem Parameter

<?php 

         require 'databaseconnect.php'; 
         $filterstmt = ("SELECT * FROM Inventory"); 
         if (!empty($_POST['ID'])): 
          $filterstmt .= (" WHERE ID = :id"); 

         endif; 
         if (!empty($_POST['ItemCode'])): 
          $filterstmt .= (" WHERE Item = :code"); 

         endif; 
         if (!empty($_POST['Type'])): 
          $filterstmt .= (" WHERE Type = :type"); 

         endif; 
         if (!empty($_POST['Condition'])): 
          $filterstmt .= (" WHERE PartCondition = :condition"); 

         endif; 
         if (!empty($_POST['Location'])): 
          $filterstmt .= (" WHERE Location = :loc"); 

         endif; 

         $preparedfilterstmt = $conn->prepare($filterstmt); 
         if (!empty($_POST['ID'])): 
         $preparedfilterstmt->bindParam(':id', $_POST['ID']); 
         endif; 
         if (!empty($_POST['ItemCode'])): 
         $preparedfilterstmt->bindParam(':code', $_POST['ItemCode']); 
         endif; 
         if (!empty($_POST['Type'])): 
         $preparedfilterstmt->bindParam(':type', $_POST['Type']); 
         endif; 
         if (!empty($_POST['Condition'])): 
         $preparedfilterstmt->bindParam(':condition', $_POST['Condition']); 
         endif; 
         if (!empty($_POST['Location'])): 
         $preparedfilterstmt->bindParam(':loc', $_POST['Location']); 
         endif; 

und dann führt dieser Teil die vorbereitete Anweisung und erstellt die Tabelle:

     $preparedfilterstmt->execute(); 
         $fltrtest = $preparedfilterstmt->rowCount(); 
         if($fltrtest > 0): 
          echo ("<h3 class = 'Title'>Search Results: </h3>"); 

        echo ("<table class = 'hubTable'> <tr class = 'tableheader'> <td class = 'hubCell'>ID</td> <td class = 'hubCell'>Item</td><td class = 'hubCell'>Type</td> <td class = 'hubCell'>Condition</td> <td class = 'hubCell'>Location</td> </tr> "); 

        while ($result = $preparedfilterstmt->fetch(PDO::FETCH_ASSOC)){ 
        echo("<tr>"."<td class = 'hubCell'><a href = 'editinventroy.php?id=".$result['ID']."'>".$result['ID']."</a> </td> 
         <td class = 'hubCell'>".$result['Item']." </td> 
         <td class = 'hubCell'>".$result['Type']." </td> 
         <td class = 'hubCell'>".$result['PartCondition']." </td> 
         <td class = 'hubCell'>".$result['Location']." </td> 

         </tr>"); 
        }  
        echo ("</table>"); 
         else: 
          echo("<div class='alert alert-warning' role='alert'><b>Hmm...</b> Nothing seems to be under those parameters</div>"); 
         endif; 

ich versuchte, einen try-catch anstelle der if Anweisungen zum Binden von Parametern, aber das hat nicht funktioniert. Ich weiß nicht, was genau hier falsch ist. Vielen Dank!

Antwort

0

Sie sind nicht verbindlich param richtig. Query sollte immer param wit and/or haben. So sollten Sie

$filterstmt = "SELECT * FROM Inventory WHERE"; 
$condition = ""; 
if (!empty($_POST['ID'])): 
    $condition .= (" ID = :id"); 
endif; 
if (!empty($_POST['ItemCode'])): 
    if (empty($condition)) { 
     $condition .= (" Item = :code"); 
    } else { 
     $condition .= (" AND Item = :code"); //use AND/Or according to your query 
    } 

endif; 
//remaining code 

Dann

$filterstmt = $filterstmt . $condition; 
0

Versuchen Sie verwenden, um mit diesem

$filterstmt = ("SELECT * FROM Inventory Where 1==1 "); 
        if (!empty($_POST['ID'])): 
         $filterstmt .= (" AND ID = :id"); 

        endif; 
        if (!empty($_POST['ItemCode'])): 
         $filterstmt .= (" AND Item = :code"); 

        endif; 
        if (!empty($_POST['Type'])): 
         $filterstmt .= (" AND Type = :type"); 

        endif; 
        if (!empty($_POST['Condition'])): 
         $filterstmt .= (" AND PartCondition = :condition"); 

        endif; 
        if (!empty($_POST['Location'])): 
         $filterstmt .= (" AND Location = :loc"); 

        endif; 
+0

Warum sollte die OP "versuchen, diese"? Eine *** gute Antwort *** wird immer eine Erklärung haben, was getan wurde und warum es so gemacht wurde, nicht nur für das OP, sondern auch für zukünftige Besucher von SO. –

Verwandte Themen