1

I want to be able to use a value from a select in my php code, but every time i get a notice saying undefined index categorie, what am I doing wrong? I've searched stuff similar to this and they all tell me to do what I'm doing already, can someone help?

<select name="categorie" value="all">
                    <option value="all">All</option>
                    <option value="action">Action</option>
                    <option value="romance">Romance</option>
                    <option value="comedy">Comedy</option>
                    <option value="drama">Drama</option>
                    <option value="horror">Horror</option>
                    <option value="fantasy">Fantasy</option>
                    <option value="scifi">Sci-fi</option>
                </select>
                <br>
                <form method="post" action="">
                <input type="submit" value="Search" name="search">
                </form>
                <?php
                $db = new PDO('mysql:host=localhost;dbname=id1552202_accounts', 'id1552202_thecouch', 'Fargo123');   
                if(isset($_POST['search'])){
                   $categorie = $_POST['categorie'];
                    if( $categorie === "all"){
                        $all = $db->prepare("SELECT name, path FROM Reviews ORDER BY date");   
             $all->execute();
        while($result = $all->fetch(PDO::FETCH_ASSOC)){
         echo $result['name'];
            echo '<br>';
        echo $result['path'];
         }
                    } else 

                    $selectreview = $db->prepare("SELECT name, path FROM Reviews WHERE genre = :categorie ORDER BY date");   
                    $selectreview->bindParam("categorie", $categorie);
             $selectreview->execute();
        while($result = $selectreview->fetch(PDO::FETCH_ASSOC)){
         echo $result['name'];
            echo '<br>';
            echo '<br>';
        echo $result['path'];
         }
        }

2 Answers2

2

Your category is not defined, that why you're experiencing that error, you must define that. Filtered well within first conditional, but need to improve in the else statement. Other point is the select outside form: in that way it will never get the value of the select, put inside the form like @addie said.

                $selectreview = $db->prepare("SELECT name, path FROM Reviews WHERE genre = :categorie ORDER BY date");   



                $selectreview->bindParam("categorie", $categorie);



         $selectreview->execute();
    while($result = $selectreview->fetch(PDO::FETCH_ASSOC)){
     echo $result['name'];
        echo '<br>';
        echo '<br>';
    echo $result['path'];
     }

My suggestion is, in the else, take the reviews without categorie bind, just to fill your frontend before the user select one to filter!

} else 

                    $selectreview = $db->prepare("SELECT name, path FROM Reviews ORDER BY date, categorie");   

             $selectreview->execute();
        while($result = $selectreview->fetch(PDO::FETCH_ASSOC)){
         echo $result['name'];
            echo '<br>';
            echo '<br>';
        echo $result['path'];
         }
capcj
  • 1,535
  • 1
  • 16
  • 23
1

place <form> on top of html, worked for me

<form method="post" action="">
    <select name="categorie">
    <option value="all">All</option>
    <option value="action">Action</option>
    <option value="romance">Romance</option>
    <option value="comedy">Comedy</option>
    <option value="drama">Drama</option>
    <option value="horror">Horror</option>
    <option value="fantasy">Fantasy</option>
    <option value="scifi">Sci-fi</option>
    </select>
<input type="submit" value="Search" name="search">
</form>
Adnan
  • 101
  • 2
  • 10