【问题标题】:SQLite statement won't take multiple valuesSQLite 语句不会采用多个值
【发布时间】:2016-03-02 15:52:30
【问题描述】:

我已经查看了很多问题,如果有任何问题可以提供帮助,请原谅我,但我无法弄清楚这一点。

所以我有一个简单的 HTML 表单,它接受用户输入的三个类别:多人游戏、平台和流派。这是html代码

    <form method="post" action="gamebuddy.php">

<div class="players">
    Please select one (or both) of the following: <br>

        <input type="checkbox" name="players[]" value="No">Single Player<br>
        <input type="checkbox" name="players[]" value="Yes">Multiplayers<br>
</div>

<div class="platform">
    Please select your game system(s): <br>

        <input type="checkbox" name="platform[]" value="XBOX">Xbox<br>
        <input type="checkbox" name="platform[]" value="PS3">PS3<br>
        <input type="checkbox" name="platform[]" value="PC">PC<br>
        <input type="checkbox" name="platform[]" value="Wii">Wii<br>
</div>


<div class="genre">
    Please select the genre(s) of game you would like: <br>
        <input type="checkbox" name="genre[]" value="Action" >Action<br>
        <input type="checkbox" name="genre[]" value="Casual">Casual<br>
        <input type="checkbox" name="genre[]" value="Roleplaying">Role-Playing<br>
        <input type="checkbox" name="genre[]" value="Shooter">Shooter<br>
        <input type="checkbox" name="genre[]" value="Sports">Sports<br>
</div>

<div class="submit">

        <input type="submit">

</div>

然后我有一个 PHP 文件,当用户单击提交时使用该文件。理想情况下,它将表单输入作为变量,并使用 SQLite 语句根据用户的选择查找用户可以玩的游戏。

这是 PHP 代码:

    <div class="displaygames">
        Based on your choices, these games seem suitable for you: <br>


<?php

if(!empty($_POST['players'])) {
    foreach($_POST['players'] as $players) {
            echo $players; //echoes the value set in the HTML form for each checked checkbox.
                         //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                         //in your case, it would echo whatever $row['Report ID'] is equivalent to.
    }
}



if(!empty($_POST['platform'])) {
    foreach($_POST['platform'] as $platform) {
            echo $platform; //echoes the value set in the HTML form for each checked checkbox.
                         //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                         //in your case, it would echo whatever $row['Report ID'] is equivalent to.
    }
}



if(!empty($_POST['genre'])) {
    foreach($_POST['genre'] as $genre) {
            echo $genre; //echoes the value set in the HTML form for each checked checkbox.

    }
}

                //This is to connect to the database
  $db = new SQLite3('gamebuddy.db');
                //Statement that uses variables to create list
$results = $db->query("SELECT * FROM games where multiplayer = '$players' and platform = '$platform' and genre is '$genre'");



                //Displays List
while ($row = $results->fetchArray()) {
        echo '<ul>';
        echo $row['game'];

        echo '</ul>';
}





?>

因此,如果您为每个类别输入一个答案,一切都会正常进行(例如,用户单击“否”进入多人游戏,“PS3”进入平台,“动作”进入类型)。但是如果用户选择“动作”和“角色扮演”,由于某种原因,它只需要用户选择的最后一个,在这种情况下是“角色扮演”。

所以我的问题是,当有多个输入时,如何获得显示所有游戏的语句。

感谢您的帮助,我会回答可能存在的任何问题,如果有帮助,当然将答案标记为已解决。谢谢!

【问题讨论】:

    标签: php html sql sqlite


    【解决方案1】:

    如果你有数组,例如 $players,你可以在 SQL 中使用 implode 函数和“IN”语句:

    "SELECT * FROM games  WHERE multiplayer IN (".implode(",", $players).")"
    

    【讨论】:

    • 我认为当您这样做时,它会将其更改为类似(“动作,随意”),而您正在尝试做的事情(这可能会使它起作用)是让它(“动作”、“休闲”)。
    猜你喜欢
    • 2016-06-03
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    相关资源
    最近更新 更多