我試圖創建一個表單,用戶可以使用多個過濾器搜索數據庫。我有兩個問題:如何使用多個數組查詢數據庫,因爲每個過濾器將作爲結果頁面上的數組存儲在變量中。第二個是:如何確保如果用戶留下過濾器空白,過濾器不會進入查詢?另外,用戶不需要在兩個第一文本條目中輸入任何內容。謝謝。如何在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 ?>
你知道你應該接受的答案? – Tobia