lundi 2 novembre 2020

Using Checkboxes with JQuery

I'm trying to search a database of desserts based on what ingredients the user has. By using 'and' in my query, even if a user checks all available ingredients, it doesn't output any desserts from the database (because none of the desserts use all of the ingredients). The database table has null for the ingredients columns that a dessert doesn't have. How can I adjust my code so that it will show all of the desserts that only use the ingredients input by the user?

<!DOCTYPE html>
<head></head>
<body>
<h1> Immediate Ingredients </h1>
    
    <form>
        Eggs <input type="checkbox" name="eggs" value="yes">
        Chocolate Chips <input type="checkbox" name="cchip" value="yes">
        Butter <input type="checkbox" name="butter" value="yes">
        Sugar <input type="checkbox" name="sugar" value="yes">
        Flour <input type="checkbox" name="flour" value="yes">
        Vanilla <input type="checkbox" name="vanilla" value="yes">
        Heavy Cream <input type="checkbox" name="hc" value="yes">
        Baking Powder <input type="checkbox" name="bpowder" value="yes">
        Peanut Butter <input type="checkbox" name="pbutter" value="yes">
        Powdered Sugar<input type="checkbox" name="psugar" value="yes">
        Fruit <input type="checkbox" name="fruit" value="yes">
        
        <input type="submit">
    </form>

<?php
$eggs=$_GET['eggs'];
$cchip=$_GET['cchip'];
$butter=$_GET['butter'];
$sugar=$_GET['sugar'];
$flour=$_GET['flour'];
$vanilla=$_GET['vanilla'];
$hc=$_GET['hc'];
$bpowder=$_GET['bpowder'];
$pbutter=$_GET['pbutter'];
$psugar=$_GET['psugar'];
$fruit=$_GET['fruit'];


$database = new PDO('sqlite:ingredients.db');
$result = $database->query("SELECT * FROM ingredients_data WHERE eggs='{$eggs}' AND cchip='{$cchip}' AND butter='{$butter}' AND sugar='{$sugar}' AND flour='{$flour}' AND vanilla='{$vanilla}' AND hc='{$hc}' AND bpowder='{$bpowder}' AND pbutter='{$pbutter}' AND psugar={$psugar}' AND fruit='{$fruit}'");
$data = $result->fetchAll (PDO::FETCH_ASSOC);
    
    foreach($data as $row_index => $ingredients){
    $row_number = $row_index + 1;
        
    echo "<h1> {$row_number}</h1>";
    echo "<h2> {$ingredients['dname']} </h2>";
    }
    


?>

</body>




Aucun commentaire:

Enregistrer un commentaire