SQLite语句获胜';t取多个值


SQLite statement won't take multiple values

我已经看了很多问题,所以如果有可能帮助的话,请原谅我,但我一直没能弄清楚。

所以我有一个简单的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",对类型点击"动作")。但是,如果用户选择"动作"answers"角色扮演",出于某种原因,它只会选择用户选择的最后一个,在这种情况下是"角色扮演"。

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

谢谢你的帮助,我会回答任何可能的问题,当然,如果有帮助的话,我会把答案标记为已解决。谢谢

如果你在$players中有数组,你可以在SQL:中使用内爆函数和"in"语句

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