2017-10-04 51 views
1

我試圖創建一個表單,用戶可以使用多個過濾器搜索數據庫。我有兩個問題:如何使用多個數組查詢數據庫,因爲每個過濾器將作爲結果頁面上的數組存儲在變量中。第二個是:如何確保如果用戶留下過濾器空白,過濾器不會進入查詢?另外,用戶不需要在兩個第一文本條目中輸入任何內容。謝謝。如何在PHP中使用多個數組進行查詢

<form action="results.php" method="post"> 
 
\t \t \t <table id="name-inputs"> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td>Common Name:</td> 
 
\t \t \t \t \t <td><input type="text" name="commonName" class="text-input" maxlength="100"></td> 
 
\t \t \t \t </tr> 
 
\t \t \t \t <tr> 
 
\t \t \t \t \t <td>Latin Name:</td> 
 
\t \t \t \t \t <td><input type="text" name="latinName" class="text-input" maxlength="100"></td> 
 
\t \t \t \t </tr> 
 
\t \t \t </table> 
 
\t \t \t <div id="plantType-container"> 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li class="listing-heading">Plant Type</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="plantType[]" value="Perennial"> Perennial</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="plantType[]" value="Annual"> Annual</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="plantType[]" value="Tree"> Tree</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="plantType[]" value="Shrub"> Shrub</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="plantType[]" value="Herb"> Herb</li> 
 
\t \t \t \t </ul> 
 
\t \t \t </div> 
 
\t \t \t <div id="lighting-container"> 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li class="listing-heading">Lighting</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="lighting[]" value="Sun"> Sun</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="lighting[]" value="Sun/Part-Sun"> Sun/Part-Sun</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="lighting[]" value="Part-Sun/Shade"> Part-Sun/Shade</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="lighting[]" value="Shade"> Shade</li> 
 
\t \t \t \t </ul> 
 
\t \t \t </div> 
 
\t \t \t <div id="water-container"> 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li class="listing-heading">Water</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="water[]" value="Average"> Dry</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="water[]" value="Dry/Average"> Dry/Average</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="water[]" value="Average"> Average</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="water[]" value="Wet/Average"> Wet/Average</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="water[]" value="Wet"> Wet</li> 
 
\t \t \t \t </ul> 
 
\t \t \t </div> 
 
\t \t \t <div id="foilage-container"> 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li class="listing-heading">Foilage</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="foilage[]" value="Decidous"> Decidous</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="foilage[]" value="Evergreen"> Evergreen</li> 
 
\t \t \t \t </ul> 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li class="listing-heading">Deer Resistant</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="deer-res[]" value="Yes"> Yes</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="deer-res[]" value="No"> No</li> 
 
\t \t \t \t </ul> 
 
\t \t \t </div> 
 
\t \t \t <div id="attracts-container"> 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li class="listing-heading">Attracts</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="butter/birds[]" value="Butterflies"> Butterflies</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="butter/birds[]" value="Hummingbirds"> Hummingbirds</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="butter/birds[]" value="Butterflies/Hummingbirds"> Butterflies/Hummingbirds</li> 
 
\t \t \t \t </ul> 
 
\t \t \t </div> 
 
\t \t \t <div id="height-container"> 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li class="listing-heading">Height</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="height[]" value='1-2"'> 1-2"</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="height[]" value='3-5"'> 3-5"</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="height[]" value='5-10"'> 5-10"</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="height[]" value='10-20"'> 10-20"</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="height[]" value="20'"> 20' +</li> 
 
\t \t \t \t </ul> 
 
\t \t \t </div> 
 
\t \t \t <div id="color-container"> 
 
\t \t \t \t <ul> 
 
\t \t \t \t \t <li class="listing-heading">Flower Color</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="color[]" value="White"> White</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="color[]" value="Pink"> Pink</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="color[]" value="Red"> Red</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="color[]" value="Blue"> Blue</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="color[]" value="Orange"> Orange</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="color[]" value="Yellow"> Yellow</li> 
 
\t \t \t \t \t <li class="listing"><input type="checkbox" name="color[]" value="Purple"> Purple</li> 
 
\t \t \t \t </ul> 
 
\t \t \t </div> 
 
\t \t \t <button type="submit" id="sButton">Submit</button> 
 
\t \t </form>

<?php 
 
\t $commonName = $_POST['commonName']; 
 
\t $latinName = $_POST['latinName']; 
 
\t $plantType = $_POST['plantType']; 
 
\t $lighting = $_POST['lighting']; 
 
\t $water = $_POST['water']; 
 
\t $foilage = $_POST['foilage']; 
 
\t $attracts = $_POST['attracts']; 
 
\t $height = $_POST['height']; 
 
\t $color = $_POST['color']; 
 
?> 
 

 
<?php 
 
\t \t $con = new mysqli("localhost", "root", "root", "Plants"); 
 
\t \t if($con->connect_error()) { 
 
\t \t \t echo "Connection Error: " . $con->connect_error(); 
 
\t \t } else { 
 
\t \t \t $sql = "SELECT * FROM plants where plantType like '%$plantType%' AND lighting like '%$lighting%' AND water like '%$water%' AND foilage like '%$foilage%' AND attracts like '%$attracts%' AND height like '%$height%' AND color like '%$color%'"; 
 
\t \t \t $res=$con->query($sql); 
 

 
\t \t \t echo "<table id='result-table'>"; 
 
\t \t \t while ($res = $con->fetch_assoc()) { 
 
\t \t \t \t echo "<tr>"; 
 
\t \t \t \t echo "<td>" . $row['commonName'] . "</td>"; 
 
\t \t \t \t echo "<td>" . $row['latinName'] . "</td>"; 
 
\t \t \t \t echo "<td>" . $row['plantType'] . "</td>"; 
 
\t \t \t \t echo "<td>" . $row['lighting'] . "</td>"; 
 
\t \t \t \t echo "<td>" . $row['water'] . "</td>"; 
 
\t \t \t \t echo "<td>" . $row['foilage'] . "</td>"; 
 
\t \t \t \t echo "<td>" . $row['attracts'] . "</td>"; 
 
\t \t \t \t echo "<td>" . $row['height'] . "</td>"; 
 
\t \t \t \t echo "<td>" . $row['color'] . "</td>"; 
 
\t \t \t \t echo "</tr>"; 
 
\t \t \t } 
 
\t \t \t echo "</table>"; 
 
\t \t } 
 
\t ?>

+0

你知道你應該接受的答案? – Tobia

回答

0

我試試下面的代碼。

<?php 

$commonName = $_POST['commonName']; 
$latinName = $_POST['latinName']; 

$queryStr = ''; 
if(isset($_POST['plantType'])){ 
    $plantType = $_POST['plantType']; 
    $queryStr .= " AND plantType like '%$plantType%'"; 
} 
if(isset($_POST['lighting'])){ 
    $lighting = $_POST['lighting']; 
    $queryStr .= " AND lighting like '%$lighting%'"; 
} 

if(isset($_POST['water'])){ 
    $water = $_POST['water']; 
    $queryStr .= " AND water like '%$water%'"; 
} 

if(isset($_POST['foilage'])){ 
    $foilage = $_POST['foilage']; 
    $queryStr .= " AND foilage like '%$foilage%'"; 
} 

if(isset($_POST['attracts'])){ 
    $attracts = $_POST['attracts']; 
    $queryStr .= " AND attracts like '%$attracts%'"; 
} 

if(isset($_POST['height'])){ 
    $height = $_POST['height']; 
    $queryStr .= " AND height like '%$height%'"; 
} 

if(isset($_POST['color'])){ 
    $color = $_POST['color']; 
    $queryStr .= " AND color like '%$color%'"; 
} 

$con = new mysqli("localhost", "root", "root", "Plants"); 
if($con->connect_error()) { 
    echo "Connection Error: " . $con->connect_error(); 
} else { 
    $sql = "SELECT * FROM plants where 1=1 '$queryStr'"; 
    $res=$con->query($sql); 

    echo "<table id='result-table'>"; 
    while ($res = $con->fetch_assoc()) { 
     echo "<tr>"; 
     echo "<td>" . $row['commonName'] . "</td>"; 
     echo "<td>" . $row['latinName'] . "</td>"; 
     echo "<td>" . $row['plantType'] . "</td>"; 
     echo "<td>" . $row['lighting'] . "</td>"; 
     echo "<td>" . $row['water'] . "</td>"; 
     echo "<td>" . $row['foilage'] . "</td>"; 
     echo "<td>" . $row['attracts'] . "</td>"; 
     echo "<td>" . $row['height'] . "</td>"; 
     echo "<td>" . $row['color'] . "</td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
} 

?>

+0

$ _POST ['plantType']和$ _POST中的每個其他條目是一個數組,這是行不通的。 –

+0

讓rstone14被指定,因爲如果它是數組,那麼他/她從未在查詢中使用Like。我100%肯定$ _POST ['plantType']不是一個數組。 –

+0

不,這是一個數組,'cos如果表單中的名稱看起來像這樣 - plantType [],這意味着它有多個值,這是一個數組。在這種情況下,$ _POST的片段將如下所示: 'Array([0] => Perennial [1] => Tree)' –

0

我試試下面的代碼

<?php 

$commonName = $_POST['commonName']; 
$latinName = $_POST['latinName']; 

$queryStr = ''; 
if(isset($_POST['plantType'])){ 
    $plantType = implode("','",$_POST['plantType']); 
    $queryStr .= " AND plantType IN ('".$plantType."') "; 
} 
if(isset($_POST['lighting'])){ 
    $lighting = implode("','",$_POST['lighting']); 
    $queryStr .= " AND lighting IN ('".$lighting."') "; 
} 

if(isset($_POST['water'])){ 
    $water = implode("','",$_POST['water']); 
    $queryStr .= " AND water IN ('".$water."') "; 
} 

if(isset($_POST['foilage'])){ 
    $foilage = implode("','",$_POST['foilage']); 
    $queryStr .= " AND foilage IN ('".$foilage."') "; 
} 

if(isset($_POST['attracts'])){ 
    $attracts = implode("','",$_POST['attracts']); 
    $queryStr .= " AND attracts IN ('".$attracts."') "; 
} 

if(isset($_POST['height'])){ 
    $height = implode("','",$_POST['height']); 
    $queryStr .= " AND height IN ('".$height."') "; 
} 

if(isset($_POST['color'])){ 
    $color = implode("','",$_POST['color']); 
    $queryStr .= " AND color IN ('".$color."') "; 
} 

$con = new mysqli("localhost", "root", "root", "Plants"); 
if($con->connect_error()) { 
    echo "Connection Error: " . $con->connect_error(); 
} else { 
    $sql = "SELECT * FROM plants where 1=1 '$queryStr'"; 
    $res=$con->query($sql); 

    echo "<table id='result-table'>"; 
    while ($res = $con->fetch_assoc()) { 
     echo "<tr>"; 
     echo "<td>" . $row['commonName'] . "</td>"; 
     echo "<td>" . $row['latinName'] . "</td>"; 
     echo "<td>" . $row['plantType'] . "</td>"; 
     echo "<td>" . $row['lighting'] . "</td>"; 
     echo "<td>" . $row['water'] . "</td>"; 
     echo "<td>" . $row['foilage'] . "</td>"; 
     echo "<td>" . $row['attracts'] . "</td>"; 
     echo "<td>" . $row['height'] . "</td>"; 
     echo "<td>" . $row['color'] . "</td>"; 
     echo "</tr>"; 
    } 
    echo "</table>"; 
}?> 
相關問題