Revolutsio Posted June 29, 2022 Share Posted June 29, 2022 I have made a database with my game collection. I have got an index page with all games on and made a dynamic link to take me to the 'details.page' which gets all the information from that particular game that clicked on and then it gets all the information into boxes and I have made a link for the year of release for the game and when I click this it shows all the games from that year (I added the year to the database in varchar in the format 1999). and then I tried to move on the next link which is Genre and i did the same in the database entered it as varchar, so for example 'Shooter' but when I click on this link and use the same code as I did with the year page I get an error 'Unknown column 'Shooter' in 'where clause' This is the link. <a class='knil' href='genre.php?id=<?php echo $game['genre']; ?>'> So on my genre page I have used at the top of the page. <?php include 'includes/config2.php'; $id = $_GET['id']; When I echo this out I get the right answer. But when I try to get all the games that have 'Shooter' in the genre column I get the error from above by using the following $sql = "SELECT * FROM games WHERE genre = $id"; $res = $db->query($sql) or die($db->error); while ($row = $res->fetch_assoc()) { // echo $row['game']; echo "<div class='picture bottom_margin'><img class='img' src='images/" . $row['image_name'] ."'> <div class='top-margin'></div> <div class='text text-shadow box-shadow bottom_margin text_center'> <a class='knil' href='details.php?id={$row['id']}'</a>{$row['game']}</a> {$row['id']} </div></a> </div> "; } In the $sql line if i change the $id to 'Shooter' I get all the games but would like to be able to get link I clicked. I know some of this code may not be the best, but this is a personal project and will not be going on the internet, Just trying to learn as I go along. Quote Link to comment https://forums.phpfreaks.com/topic/314971-unknown-column-in-where-clause-and-how-to-fix-it/ Share on other sites More sharing options...
Solution Barand Posted June 29, 2022 Solution Share Posted June 29, 2022 If $id contains "Shooter" as a string value it need to be in quotes otherwise SQL thinks it is a column name $sql = "SELECT * FROM games WHERE genre = '$id' "; ^ ^ 1 Quote Link to comment https://forums.phpfreaks.com/topic/314971-unknown-column-in-where-clause-and-how-to-fix-it/#findComment-1597703 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.