简体   繁体   English

使用复选框创建SQL查询

[英]Creating a SQL Query with checkboxes

I am new to SQL and PHP and trying to run a query. 我是SQL和PHP的新手,正在尝试运行查询。

I am struggling to put the correct operators in, and can't figure out why it won't work! 我正竭尽全力地将正确的运算符放进去,并且不知道为什么它不起作用! I've been using AND OR CASE 我一直在使用AND OR CASE

I have 4 checkboxes: Bars, Clubs, Restaurants, Pubs. 我有4个复选框:酒吧,俱乐部,餐馆,酒吧。

In my database I have a name column and a column for each of the 4 words on my radio buttons. 在我的数据库中,我的单选按钮上有一个“名称”列和一个四个单词的每个列。 If the establishment is a Bar, then there is a 1 in the corresponding Bar column. 如果场所是酒吧,则在相应的酒吧列中为1。 If the establishment is a Pub, then there is a 1 in the corresponding Pub column. 如果企业是Pub,则在相应的Pub列中为1。 If it is both then it will have a 1 in both etc etc 如果两者都是,那么两者都将为1,以此类推

This is the sort of thing it looks like: 它看起来像这样:

Name  | Bar   | Club | Restaurant | Pub
———————————————————————————————————————
McD   |  0    |  0   |      1     |  0
KFC   |  1    |  0   |      1     |  0
SPN   |  1    |  1   |      1     |  1
GRG   |  0    |  1   |      1     |  0

When there is nothing checked I would like the result to show all the names of options. 当没有任何检查时,我希望结果显示所有选项名称。 When Bars is checked then Only the Bars When Clubs is checked then Only the Clubs etc etc 如果选中“酒吧”,则仅选中“酒吧”,然后选中“俱乐部”等

When Bars and Clubs are checked then I want to see all the Bars and Clubs etc etc 选中Bars and Clubs之后,我要查看所有Bars and Clubs等

How do I get the the outcome I am looking for? 如何获得想要的结果? Thanks so much for your help in advance! 非常感谢您的提前帮助!

This is the code I am working with at the moment (I have left the Bar variable in) : 这是我目前正在使用的代码(我将Bar变量留在了其中):

<?php

error_reporting(E_ALL);

$servername = "**";
$username = "**";
$password = "**";
$dbname = "**";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(isset($_POST['submit'])) {

    $getselectClub = implode(",",$_POST['selectClub']);
    $getselectResturant = implode(",",$_POST['selectResturant']);
    $getselectBar = implode(",",$_POST['selectBar']);

$sql = "SELECT id, name, locale FROM theList WHERE bar='$getselectBar' ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


?>

<?php echo $row['id'];?> &nbsp;
<?php echo $row['name'];?> &nbsp;
<?php echo $row['locale'];?><br>

<?php }}} ?>

<form method="post">
    Club: <input type='checkbox' name="selectClub[]" value="1" />
    Resturant: <input type='checkbox' name="selectResturant[]" value="1" />
    Bar: <input type='checkbox' name="selectBar[]" value="1"/>
    <input type="submit" name="submit" value="submit" />
</form> 

A checkbox is not submitted if it's unchecked. 如果未选中该复选框,则不会提交该复选框。 So you check for its submission before adding it to the query. 因此,您需要先检查其提交,然后再将其添加到查询中。 Adding [] to the end of a form input name submits it as an array, which is not something you need. 在表单输入名称的末尾添加[]会将其提交为数组,这不是您所需要的。

<?php

error_reporting(E_ALL);

$servername = "**";
$username   = "**";
$password   = "**";
$dbname     = "**";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(isset($_POST["submit"])) {
    $where = [];
    $query = "";
    if (!empty($_POST['selectClub'])) $where[] = "club=1";
    if (!empty($_POST['selectRestaurant'])) $where[] = "restaurant=1";
    if (!empty($_POST['selectBar'])) $where[] = "bar=1";

    // check in case no boxes were checked
    if (!empty($where)) {
        $query = "WHERE " . implode(" OR ", $where);
    }

    $sql = "SELECT id, name, locale FROM theList $query";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "$row[id] &nbsp; $row[name] &nbsp; $row[locale]<br/>";
        }
    }
}
?>

<form method="post">
    Club: <input type='checkbox' name="selectClub" value="1" />
    Restaurant: <input type='checkbox' name="selectRestaurant" value="1" />
    Bar: <input type='checkbox' name="selectBar" value="1"/>
    <button type="submit" name="submit" value="1">Submit</button>
</form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM