我想将复选框与SQL select语句集成,但我不知道如何将数组转换为可用的select。
<form method="post">
<input name="searchterm" type="search" placeholder="Enter Search Terms Here" />
<input name="searchbtn" type="submit" value="Karaoke Search" />
<input name="checkbx[]" type="checkbox" checked value="Chartbuster" />
<label>Chartbuster</label>
<input name="checkbx[]" type="checkbox" checked value="Sound Choice" />
<label>Sound Choice</label>
<input name="checkbx[]" type="checkbox" checked value="DKKaraoke" />
<label>DKKaraoke</label>
<input name="checkbx[]" type="checkbox" checked value="Sunfly" />
<label>Sunfly</label>
<input name="checkbx[]" type="checkbox" checked value="Karaoke Hits" />
<label>Karaoke Hits</label>
<?php
if(isset($_POST['searchterm']) and ($_POST['searchterm']!="")) {
$searchterm=$_POST['searchterm'];
$checkbx=$_POST['checkbx'];
$searchresults=$db->query("SELECT *
FROM 1KaraokeDJ
WHERE Artist LIKE '%$searchterm%'
GROUP BY Artist,
Title,
Brand
ORDER BY Artist,
Title,
Disc LIMIT 100");
}
...
?>
...
</form>
基本上,将搜索限制在所选复选框之一的Brand处。
您可以使用
$checkbx = join(',',$checkbx);
然后这个变量应该适合您的查询。
此外,请删除圆括号,它们不需要:
$searchterm=($_POST['searchterm']);
$checkbx=($_POST['checkbx']);
成为
$searchterm=$_POST['searchterm'];
$checkbx=$_POST['checkbx'];
在头疼之后,我偶然发现了一些类似问题中没有添加到任何答案中的东西。。。
选择IN('a','b','c'…)需要在每个文本选项周围使用"'"。在所有的例子中,它都假设了数字!
$checkbx=联接(',',$checkbx);起初不起作用,因为数组中的每个文本项在加入之前都必须有"。
因此,对我来说,解决方案(也许是更好的解决方案)是在我的每个复选框值中添加":
<input name="checkbx[]" type="checkbox" value="'Chartbuster'" />
<label>Chartbuster</label>
<input name="checkbx[]" type="checkbox" value="'Sound Choice'" />
<label>Sound Choice</label>
<input name="checkbx[]" type="checkbox" value="'DKKaraoke'" />
<label>DKKaraoke</label>
<input name="checkbx[]" type="checkbox" value="'Sunfly'" />
<label>Sunfly</label>
<input name="checkbx[]" type="checkbox" value="'Karaoke Hits'" />
<label>Karaoke Hits</label>
<?php
if(isset($_POST['searchterm']) and ($_POST['searchterm']!="") and isset($_POST['checkbx'])) {
$searchterm=$_POST['searchterm'];
$checkbx=join(',',$_POST['checkbx']);
$searchresults=$db->query("SELECT *
FROM 1KaraokeDJ
WHERE Artist LIKE '%$searchterm%'
AND Brand IN ($checkbx)
GROUP BY Artist,
Title,
Brand
ORDER BY Artist,
Title,
Disc
LIMIT 100");
}
...
?>
...