我在我的網站上有4個複選框的搜索功能。然後將它們粘貼到下一頁,我要查找與複選框的標準相匹配的所有產品。在select語句中有4'ands'
因爲我有4個複選框,我想用4「ANDS」,但我認爲3是最大(?)
我如何解決此得到如此它搜索,看看是否所有的產品都匹配?
HTML表單
<div id = "search">
<form name = search action = "search.php" method = "POST">
<p class = "big"> Refine Menu </p>
<hr>
<input type = "text" name = "search" placeholder = "Search for an item" size = "12">
<input type = "submit" value = "Go">
<br><br>
<input type = "checkbox" name = "vegetarian"> Vegetarian
<br><input type = "checkbox" name = "vegan"> Vegan
<br><input type = "checkbox" name = "coeliac"> Coeliac
<br><input type = "checkbox" name = "nutFree"> Nut free
</form>
</div>
PHP
<?php
session_start();
include "connection.php";
if(!isset($_SESSION["username"])){
header("Location: login.php");
}
if(isset($_POST["search"])){
$search = $_POST["search"];
}
if(isset($_POST["vegetarian"])){
$vegetarian = 1;
}
else{
$vegetarian = NULL;
}
if(isset($_POST["vegan"])){
$vegan = 1;
}
else{
$vegan = NULL;
}
if(isset($_POST["coeliac"])){
$coeliac = 1;
}
else{
$coeliac = NULL;
}
if(isset($_POST["nutFree"])){
$nutFree = 1;
}
else{
$nutFree = NULL;
}
$sql = "SELECT * FROM products WHERE vegan = '$vegan' and nutFree = '$nutFree' and vegetarian = '$vegetarian' and coeliac = '$coeliac'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($result)){
echo $row ["name"];
}
我已經嘗試了一些不同的東西,但我不知道的sql正確的語法。
注:在我的數據庫是否滿足在其上的requierment被保存爲1或0,這就是爲什麼我從改成了「開」或「關」
..我不知道你聽到的地方至多有3個'和'語句,但它是垃圾。嘗試一下。你得到了什麼錯誤?匆匆一瞥,你的陳述看起來很好。你的$ sql字符串在創建後看起來像什麼?您還應該考慮使用準備好的語句和綁定參數來代替字符串連接來構建您的查詢 –
您的SQL查詢是正確的... – edCoder
更正您的表單元素