我有一个带有单选按钮的表单,我想用它来从MySQL数据库中的表中获取数据,这是以下形式:
<div class="filter-container">
<p class="filter-title">BTU's</p><div class="filter">
<input type="radio" name="choice-0" id="choice-0" tabindex="0" value="" checked="checked" />
<label for="none">None</label></div><div class="filter">
<input type="radio" name="choice-0" id="choice-0" tabindex="0" value="100000" />
<label for="choice-0">100000<span>(10)</span></label>
</div><div class="filter">
<input type="radio" name="choice-0" id="choice-0" tabindex="0" value="120000" />
<label for="choice-0">120000<span>(7)</span></label>
</div><div class="filter">
<input type="radio" name="choice-0" id="choice-0" tabindex="0" value="40000" />
<label for="choice-0">40000<span>(5)</span></label>
</div><div class="filter">
<input type="radio" name="choice-0" id="choice-0" tabindex="0" value="60000" />
<label for="choice-0">60000<span>(11)</span></label>
</div><div class="filter">
<input type="radio" name="choice-0" id="choice-0" tabindex="0" value="80000" />
<label for="choice-0">80000<span>(18)</span></label>
</div></div><div class="filter-container">
<p class="filter-title">Efficiency (AFUE)</p><div class="filter">
<input type="radio" name="choice-1" id="choice-1" tabindex="0" value="" checked="checked" />
<label for="none">None</label></div><div class="filter">
<input type="radio" name="choice-1" id="choice-1" tabindex="1" value="80%" />
<label for="choice-1">80%<span>(21)</span></label>
</div><div class="filter">
<input type="radio" name="choice-1" id="choice-1" tabindex="1" value="92%" />
<label for="choice-1">92%<span>(8)</span></label>
</div><div class="filter">
<input type="radio" name="choice-1" id="choice-1" tabindex="1" value="96%" />
<label for="choice-1">96%<span>(17)</span></label>
</div><div class="filter">
<input type="radio" name="choice-1" id="choice-1" tabindex="1" value="97%" />
<label for="choice-1">97%<span>(5)</span></label>
</div></div><div class="filter-container">
<p class="filter-title">Width</p><div class="filter">
<input type="radio" name="choice-2" id="choice-2" tabindex="0" value="" checked="checked" />
<label for="none">None</label></div><div class="filter">
<input type="radio" name="choice-2" id="choice-2" tabindex="2" value="14" />
<label for="choice-2">14<span>(4)</span></label>
</div><div class="filter">
<input type="radio" name="choice-2" id="choice-2" tabindex="2" value="17.5" />
<label for="choice-2">17.5<span>(20)</span></label>
</div><div class="filter">
<input type="radio" name="choice-2" id="choice-2" tabindex="2" value="21" />
<label for="choice-2">21<span>(19)</span></label>
</div><div class="filter">
<input type="radio" name="choice-2" id="choice-2" tabindex="2" value="221" />
<label for="choice-2">221<span>(1)</span></label>
</div><div class="filter">
<input type="radio" name="choice-2" id="choice-2" tabindex="2" value="24.5" />
<label for="choice-2">24.5<span>(7)</span></label>
</div></div><div class="filter-container">
<p class="filter-title">Blower Type</p><div class="filter">
<input type="radio" name="choice-3" id="choice-3" tabindex="0" value="" checked="checked" />
<label for="none">None</label></div><div class="filter">
<input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Multi-Speed" />
<label for="choice-3">Multi-Speed<span>(23)</span></label>
</div><div class="filter">
<input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Multi-Speed ECM" />
<label for="choice-3">Multi-Speed ECM<span>(7)</span></label>
</div><div class="filter">
<input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Single Speed" />
<label for="choice-3">Single Speed<span>(7)</span></label>
</div><div class="filter">
<input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Variable Speed" />
<label for="choice-3">Variable Speed<span>(3)</span></label>
</div><div class="filter">
<input type="radio" name="choice-3" id="choice-3" tabindex="3" value="Variable Speed ECM" />
<label for="choice-3">Variable Speed ECM<span>(11)</span></label>
</div></div>
我需要将值的任意组合与类别常量匹配。这就是我用来处理表单的内容:
<?php
if('POST' == htmlspecialchars($_SERVER['REQUEST_METHOD']) ){
$cat_id = 4001;
if ( isset($_POST['choice-0']) ) {
$choice0 = $_POST['choice-0'];
}
if ( isset($_POST['choice-1']) ) {
$choice1 = $_POST['choice-1'];
}
if ( isset($_POST['choice-2']) ) {
$choice2 = $_POST['choice-2'];
}
if ( isset($_POST['choice-3']) ) {
$choice3 = $_POST['choice-3'];
}
if ( isset($_POST['choice-4']) ) {
$choice4 = $_POST['choice-4'];
}
if ( isset($_POST['choice-5']) ) {
$choice5 = $_POST['choice-5'];
}
if ( isset($_POST['choice-6']) ) {
$choice6 = $_POST['choice-6'];
}
$query = $connect->query( "SELECT * FROM PRODUCTS WHERE PR_ProductCategory='$cat_id' AND (PR_UDSearch0='$choice0' OR PR_UDSearch1='$choice1' OR PR_UDSearch2='$choice2' OR PR_UDSearch3='$choice3' OR PR_UDSearch4='$choice4' OR PR_UDSearch5='$choice5' OR PR_UDSearch6='$choice6')" );
var_dump($query);
}
&GT;
我已经做了各种SELECT语句来获取没有运气的数据(包括上面的那个)。我不想要比赛的组合。根据我的需要,我需要来自其中一个的匹配,而不是两个。换句话说,我需要将cat_id用作常量,然后匹配从单选按钮(选择1,选择1和2,或选择2和4,或选择3等)获得的任何其他组合。我必须在查询或其他方面设置错误。我已经搜索了好几天,还没有想到它。谢谢!
更新:我现在正在尝试根据选项创建SELECT语句:
$cat_id = 4001;
$query_str = "SELECT * FROM PRODUCTS WHERE PR_ProductCategory='$cat_id' AND (";
if ( isset($_POST['choice-0'] ) ) {
$choice0 = $_POST['choice-0'];
$query_str .= "PR_UDSearch0='$choice0'";
}
if ( isset($_POST['choice-1'] ) ) {
$choice1 = $_POST['choice-1'];
$query_str .= "OR PR_UDSearch0='$choice1'";
}
if ( isset($_POST['choice-2'] ) ) {
$choice2 = $_POST['choice-2'];
$query_str .= "OR PR_UDSearch0='$choice2'";
}
if ( isset($_POST['choice-3'] ) ) {
$choice3 = $_POST['choice-3'];
$query_str .= "OR PR_UDSearch0='$choice3'";
}
if ( isset($_POST['choice-4'] ) ) {
$choice4 = $_POST['choice-4'];
$query_str .= "OR PR_UDSearch0='$choice4'";
}
if ( isset($_POST['choice-5'] ) ) {
$choice5 = $_POST['choice-5'];
$query_str .= "OR PR_UDSearch0='$choice5'";
}
if ( isset($_POST['choice-6'] ) ) {
$choice6 = $_POST['choice-6'];
$query_str .= "OR PR_UDSearch0='$choice6'";
}
$query_str .= ")";
$query = $connect->query( $query_str );
我认为它更接近但如果没有选择某些东西我会得到NULL。
最后更新:我在一些帮助下想出来了。我根据选择构建了SELECT语句,并使用它来获得结果。这是我的完整解决方案:
if('POST' == htmlspecialchars($_SERVER['REQUEST_METHOD']) ){
$cat_id = 4001;
$query_str = "SELECT * FROM PRODUCTS WHERE PR_ProductCategory='$cat_id' ";
if ( isset($_POST['choice-0']) ) {
$choice0 = $_POST['choice-0'];
if ( $choice0 ) {
$query_str .= "AND PR_UDSearch0='$choice0'";
}
}
if ( isset($_POST['choice-1']) ) {
$choice1 = $_POST['choice-1'];
if ( $choice1 ) {
$query_str .= " AND PR_UDSearch1='$choice1'";
}
}
if ( isset($_POST['choice-2']) ) {
$choice2 = $_POST['choice-2'];
if ( $choice2 ) {
$query_str .= " AND PR_UDSearch2='$choice2'";
}
}
if ( isset($_POST['choice-3']) ) {
$choice3 = $_POST['choice-3'];
if ( $choice3 ) {
$query_str .= " AND PR_UDSearch3='$choice3'";
}
}
if ( isset($_POST['choice-4']) ) {
$choice4 = $_POST['choice-4'];
if ( $choice4 ) {
$query_str .= " AND PR_UDSearch4='$choice4'";
}
}
if ( isset($_POST['choice-5']) ) {
$choice5 = $_POST['choice-5'];
if ( $choice5 ) {
$query_str .= " AND PR_UDSearch5='$choice5'";
}
}
if ( isset($_POST['choice-6']) ) {
$choice6 = $_POST['choice-6'];
if ( $choice6 ) {
$query_str .= " AND PR_UDSearch6='$choice6'";
}
}
$query_str .= "";
$query = $connect->query( $query_str );
while ($row = mysqli_fetch_array($query)) {
$sku = $row[PR_SKU];
$img = $row[PR_URLofThumb];
$desc = $row[PR_Description];
$price = $row[PR_UnitPrice];
echo '<div class="box">
<div class="box-image">
<a href="/cgi-bin/commerce.cgi?preadd=action&key=' . $sku . '"><img src="' . $img . '" /></a>
</div>
<p>Price:' . $price . '</p>
<a href="/cgi-bin/commerce.cgi?preadd=action&key=' . $sku . '">' . $desc . '</a>
</div>';
}
}
答案 0 :(得分:0)
似乎您可能必须根据选择构建查询语句。这段代码就是这样做的。
$cat_id = 4001;
$choices = [];
if(!empty($_POST['choice-0']))
{
$choices[] = "PR_UDSearch0='{$_POST['choice-0']}'";
}
if(!empty($_POST['choice-1']))
{
$choices[] = "PR_UDSearch1='{$_POST['choice-1']}'";
}
if(!empty($_POST['choice-2']))
{
$choices[] = "PR_UDSearch2='{$_POST['choice-2']}'";
}
if(!empty($_POST['choice-3']))
{
$choices[] = "PR_UDSearch3='{$_POST['choice-3']}'";
}
if(!empty($_POST['choice-4']))
{
$choices[] = "PR_UDSearch4='{$_POST['choice-4']}'";
}
if(!empty($_POST['choice-5']))
{
$choices[] = "PR_UDSearch5='{$_POST['choice-5']}'";
}
if(!empty($_POST['choice-6']))
{
$choices[] = "PR_UDSearch6='{$_POST['choice-5']}'";
}
$additional_where = "";
$count = count($choices);
if($count > 0)
{
if($count > 1)
{
$i = 0;
$additional_where = " AND (";
foreach($choices as $choice)
{
$or = ++$i < $count ? " OR " : ""; //adding OR where needed
$additional_where .= $choice.$or;
}
$additional_where .= ")";
}
else
{
$additional_where = " AND $choices[0]";
}
}
$sql = "SELECT * FROM PRODUCTS WHERE PR_ProductCategory='$cat_id'".$additional_where;
var_dump($sql);
//$query = $connect->query($sql);
//var_dump($query);
通过使用!empty($_POST[...
,您确认已设置$_POST
项,它不是NULL,并且不是一次空字符串。