根据表单中的用户输入更改PHP文档中的SQL查询很困难

根据表单中的用户输入更改PHP文档中的SQL查询很困难,php,mysql,sql,pear,Php,Mysql,Sql,Pear,我有一个PHP文档,它根据搜索表单的输入值查询MYSQL数据库。一切都很完美,除了我正在努力增加一个功能,我相信这将使整个应用程序更加用户友好 <form> <label for="alcohol_check">Search clubs where alcohol permitted?</label> <input type="radio" id="alcohol_check1" value="1" /><label f


我有一个PHP文档,它根据搜索表单的输入值查询MYSQL数据库。一切都很完美,除了我正在努力增加一个功能,我相信这将使整个应用程序更加用户友好

<form>
    <label for="alcohol_check">Search clubs where alcohol permitted?</label>
       <input type="radio" id="alcohol_check1" value="1" /><label for="radio1">No</label>
       <input type="radio" id="alcohol_check2" value="2" /><label for="radio2">Yes</label>
       <input type="radio" id="alcohol_check3" value="???" /><label for="radio3">Show both</label>

    <label for="club_name">Enter social club name:</label>
       <input type="text" id="club_name" name="club_name"/>
</form>
$user_alcohol_permitted_selection = $_POST['alcohol_check']; //Value sent using jquery .load()
$user_social_club_name_input = $_POST['name']; //Value sent using jquery .load()

$query="SELECT * FROM social_clubs 
        WHERE name = $user_social_club_name_input
        AND
        WHERE alcohol_permitted = $user_alcohol_permitted_selection";
我数据库中的表基本上存储了各种社交俱乐部的信息。它有关于他们的位置、姓名、订票价格和允许饮酒的数据。我需要帮助的部分与酒精有关。在数据库中,its存储1或2,2表示允许饮酒,1表示不允许饮酒

<form>
    <label for="alcohol_check">Search clubs where alcohol permitted?</label>
       <input type="radio" id="alcohol_check1" value="1" /><label for="radio1">No</label>
       <input type="radio" id="alcohol_check2" value="2" /><label for="radio2">Yes</label>
       <input type="radio" id="alcohol_check3" value="???" /><label for="radio3">Show both</label>

    <label for="club_name">Enter social club name:</label>
       <input type="text" id="club_name" name="club_name"/>
</form>
$user_alcohol_permitted_selection = $_POST['alcohol_check']; //Value sent using jquery .load()
$user_social_club_name_input = $_POST['name']; //Value sent using jquery .load()

$query="SELECT * FROM social_clubs 
        WHERE name = $user_social_club_name_input
        AND
        WHERE alcohol_permitted = $user_alcohol_permitted_selection";
当前我的表单如下所示:

<form>
    <label for="alcohol_check">Search clubs where alcohol permitted?</label>
       <input type="radio" id="alcohol_check1" value="1" /><label for="radio1">No</label>
       <input type="radio" id="alcohol_check2" value="2" /><label for="radio2">Yes</label>
       <input type="radio" id="alcohol_check3" value="???" /><label for="radio3">Show both</label>

    <label for="club_name">Enter social club name:</label>
       <input type="text" id="club_name" name="club_name"/>
</form>
$user_alcohol_permitted_selection = $_POST['alcohol_check']; //Value sent using jquery .load()
$user_social_club_name_input = $_POST['name']; //Value sent using jquery .load()

$query="SELECT * FROM social_clubs 
        WHERE name = $user_social_club_name_input
        AND
        WHERE alcohol_permitted = $user_alcohol_permitted_selection";
目前的问题是,我的查询将只检索提供酒精或不提供酒精的社交俱乐部。因为此时从允许形式传递的值为1或2。 我可以对我的查询做些什么,以便如果用户选择“两者”选项,它检索提供酒精的社交俱乐部和不提供酒精的俱乐部?我想不出来!查询中是否需要if语句?

<form>
    <label for="alcohol_check">Search clubs where alcohol permitted?</label>
       <input type="radio" id="alcohol_check1" value="1" /><label for="radio1">No</label>
       <input type="radio" id="alcohol_check2" value="2" /><label for="radio2">Yes</label>
       <input type="radio" id="alcohol_check3" value="???" /><label for="radio3">Show both</label>

    <label for="club_name">Enter social club name:</label>
       <input type="text" id="club_name" name="club_name"/>
</form>
$user_alcohol_permitted_selection = $_POST['alcohol_check']; //Value sent using jquery .load()
$user_social_club_name_input = $_POST['name']; //Value sent using jquery .load()

$query="SELECT * FROM social_clubs 
        WHERE name = $user_social_club_name_input
        AND
        WHERE alcohol_permitted = $user_alcohol_permitted_selection";

谢谢你检查这个,如果我没有解释清楚的话,很抱歉

好的,首先,请将alchohol_允许列的数据类型更改为bool(MySQL中的位(1))。只是读书让我觉得很冷

现在,谈谈你的问题。尝试以下where语句:

WHERE $user_alcohol_permitted_selection IS NULL 
OR alcohol_permitted = $user_alcohol_permitted_selection
这应该行得通

$user_alcohol_permitted_selection = $_POST['alcohol_check']; //Value sent using jquery .load()
$user_social_club_name_input = $_POST['name']; //Value sent using jquery .load()

$query="SELECT * FROM social_clubs 
        WHERE name = $user_social_club_name_input";

if ($user_alcohol_permitted_selection == "???")
{
     $query.= "AND WHERE alcohol_permitted = 1 OR alcohol_permitted = 2";
} else {
     $query.= "AND WHERE alcohol_permitted = $user_alcohol_permitted_selection";
}
此外,如果列
仅包含值1或2,则允许使用该列。我建议您将列类型更改为bool,就像TheAllmitchConnors在回答中建议的那样

然后,可以将条件语句缩减为:

$query="SELECT * FROM social_clubs 
        WHERE name = $user_social_club_name_input";    

if ($user_alcohol_permitted_selection != "???")
{
     $query.= "AND WHERE alcohol_permitted = $user_alcohol_permitted_selection";
}

请注意

您确实需要阅读并开始使用准备好的语句来避免SQL注入。
其中酒精允许=1和酒精允许=2
应该是
x=1和x=2总是假的:)
if(!$user\u alcohold\u allowed\u selection==“?”)
应该是
if($user\u alcohold\u allowed\u selection!=“?”)
,而我们在这里。@FrankFarmer甚至想不出任何借口。我能怪JavaScript还是C?