Axon
Axon

Reputation: 3

Search depending on tags

I got these 2 tables

        POSTS                         TAGS
[  id    -     title  ]        [ pid   -   tag ]
[  1     -  HelloWorld]        [ 1     -   PHP ]
[  2     -    Arrays  ]        [ 2     -   PHP]
[  3     -     AJAX   ]        [ 3     -   JQ  ]

and this Searchbox

<form action="" method="post">
    <select name="tags">
        <option value="PHP">PHP</option>
        <option value="Echo">Echo</option>
        <option value="JQ">JQ</option>
    </select>
    <input type="submit" name="submit">
</form>

now to my PHP code

if(isset($_POST['submit']))
    $tag = $_POST['tags'];

    $query = "
    SELECT a.title FROM posts a
    INNER JOIN tags b ON a.id = b.pid
    WHERE b.tag = :tag
    ";

    $stmt = $pdo->prepare($query);
    $stmt->execute([':tag' => $tag]);
    while($row = $stmt->fetch()){
        $title = $row['title'];
        echo $title;
    }

but nothing happens after clicking submit, How can i get both id 1 & 2 titles when i select PHP tag in my html code?

Upvotes: 0

Views: 21

Answers (1)

Inna Tichman
Inna Tichman

Reputation: 626

you don't have a variable named $data in you while, its $row:

$stmt->execute(['tag' => $tag]);
while($row = $stmt->fetch()){
        $title = $row['title'];
        echo $title;
}

Upvotes: 1

Related Questions