Php 从多个复选框形成查询字符串

Php 从多个复选框形成查询字符串,php,sql,checkbox,Php,Sql,Checkbox,我试图从多个复选框中形成一个查询字符串,用于查询我的数据库 我有以下表格: <fieldset data-role="controlgroup"> <input type="checkbox" name="wheat" id="checkbox-1a" class="custom" /> <label for="checkbox-1a">Wheat Allergy</label&g

我试图从多个复选框中形成一个查询字符串,用于查询我的数据库

我有以下表格:

            <fieldset data-role="controlgroup">

            <input type="checkbox" name="wheat" id="checkbox-1a" class="custom" />
            <label for="checkbox-1a">Wheat Allergy</label>

            <input type="checkbox" name="yeast" id="checkbox-2a" class="custom" />
            <label for="checkbox-2a">Yeast Allergy</label>

            <input type="checkbox" name="sugar" id="checkbox-3a" class="custom" />
            <label for="checkbox-3a">Sugar Allergy</label>

            <input type="checkbox" name="dairy" id="checkbox-4a" class="custom" />
            <label for="checkbox-4a">Dairy Allergy</label>
这是我想要的,但不是很优雅

首先,sql查询如下所示:

从无糖=1空气自由=1的配方中选择*

如果用户选择不选择一个,我当然会得到一个未被选择的str的未定义变量错误

不知道该如何解决这个问题,也不知道下一步该怎么做。我希望这里有一些逻辑,根据表单上检查的内容修改字符串,然后形成一个漂亮的干净SQL查询,我可以对我的DB运行。但是,唉,我迷路了:(

帮助?

这个怎么样:

$options = array();
if(isset($_POST['wheat']))
{
    $options[] = 'wheatfree = 1';
}

if(isset($_POST['yeast']))
{
    $options[] = 'yeastfree = 1';
}

if(isset($_POST['sugar']))
{
    $options[] = 'sugarfree = 1';
}

if(isset($_POST['dairy']))
{
    $options[] = 'dairyfree = 1';
}

$fullsearch = implode(' AND ', $options);

$str_SQL = "SELECT * FROM recipes";
if ($fullsearch <> '') {
    $str_SQL .= " WHERE " . $fullsearch;
}

echo $str_SQL;
$options=array();
如果(isset($_POST['wheat']))
{
$options[]='wheatfree=1';
}
如果(isset($_POST['yeast']))
{
$options[]=“yeastfree=1”;
}
如果(isset($_POST['sugar']))
{
$options[]='无糖=1';
}
如果(isset($_POST['dairy']))
{
$options[]='dairyfree=1';
}
$fullsearch=内爆('AND',$options);
$str_SQL=“从配方中选择*”;
如果($fullsearch“”){
$str_SQL.=“WHERE”。$fullsearch;
}
echo$str_SQL;

关于Dave的回答:

$options = Array();
$ingredients = Array('wheat', 'yeast', 'sugar', 'dairy');

foreach ($ingredients as $i)
   if (isset($_POST[$i]))
      $options[] = $i . 'free = 1';

$sql = "SELECT * FROM recipes";
if (count($options))
   $sql .= " WHERE " . implode(' AND ', $options);

echo $sql;

但是为什么不使用复选框的
属性呢

<input type="checkbox" name="ingredients[]" value="wheat" />
<input type="checkbox" name="ingredients[]" value="sugar" />

给复选框name=“allergy[]”那么你就有了一个很好的数组,然后你可以使用内爆来生成查询stringHeh,他只是编辑它来做这件事,并在你发表评论时将str更改为options。不过,谢谢你,这是对另一条评论的回应,因为删除了。)呃,你真的需要所有这些大括号吗?我喜欢大括号。而且,我发现最好总是添加它们-你永远不知道别人什么时候会修改你的代码,也不会发现没有大括号的if…谢谢Dave,这正是我所需要的。我没想过把它放到数组中…:)@戴夫:请看我的答案,它表明不需要这个冗长和冗余的条件流。太棒了,我没想过把它放到数组中。罗尔西,谢谢@redcow:我已经扩展了我的答案,加入了一个更好的替代方案。这并不一定更好,因为它现在很容易被SQL注入(我看到您提到了escape
$I
)。我个人认为“白名单”更好。但无论如何,这只是我的意见。与原来的相比,它仍然是一个改进;)(你已经从我这里得到+1;)@Felix:是的,我不是很确定。这当然是可扩展性和合理性之间的折衷。:)
<input type="checkbox" name="ingredients[]" value="wheat" />
<input type="checkbox" name="ingredients[]" value="sugar" />
$options = Array();
foreach ($_POST['ingredients'] as $i)
   $options[] = $i . 'free = 1'; // don't forget to escape $i somehow!

$sql = "SELECT * FROM recipes";
if (count($options))
   $sql .= " WHERE " . implode(' AND ', $options);

echo $sql;