samedi 28 novembre 2015

SQLite statement won't take multiple values

I've looked through a lot of questions, so forgive me if there is one that may help, but I've not been able to figure this out.

So I have a simple HTML form, that takes the users input for three categories: multiplayer, platform, and genre. This is the html code

    <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>

And then I have a PHP file that is used when the user clicks submit. Ideally, it takes the form inputs as a variable, and uses the SQLite statement to find the games the user can play based on his choices.

Here's the PHP code:

    <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>';
}





?>

So everything works fine if you put in one answer for each category (for example, the user clicks "No" to multiplayer, "PS3" to platform, and "action" to genre). BUT if the user selects "Action" AND "Role-Playing", for some reason it only takes the last one the user selects, in this instance "role-playing".

So my question is how do I get the statement to show ALL of the games when there are multiple inputs.

Thank you for your help, I will answer any questions there may be, and of course mark the answer as solved if it helps. Thanks!

Aucun commentaire:

Enregistrer un commentaire