2017-10-04 190 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>"; 
}?> 
相关问题