oz11 Posted July 20, 2022 Share Posted July 20, 2022 (edited) <h1>Search</h1> <?php if (isset($_POST['userid'])){ $keyword = trim ($_POST['userid']); echo $keyword; $stmt = $pdo->prepare("SELECT * FROM `users` WHERE name LIKE '%?%'"); $stmt->execute([$keyword]); while ($row = $stmt->fetch()) { echo $row['user_id']."<br />\n"; } } if (isset($_POST['userstring'])){ $keyword = trim ($_POST['userstring']); echo $keyword; } if (isset($_POST['postid'])){ $keyword = trim ($_POST['postid']); echo $keyword; } if (isset($_POST['poststring'])){ $keyword = trim ($_POST['poststring']); echo $keyword; } ?> <form method="POST" action="index.php?page=search&type=user_id"> <h3>User ID</h3> <label for="lname">Username ID:</label><br> <input type="text" id="userid" name="userid" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="POST" action="index.php?page=search&type=user_string"> <h3>User string</h3> <label for="lname">Username String:</label><br> <input type="text" id="userstring" name="userstring" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="POST" action="index.php?page=search&type=post_id"> <h3>Post ID</h3> <label for="lname">Post ID:</label><br> <input type="text" id="postid" name="postid" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> <form method="POST" action="index.php?page=search&type=post_string"> <h3>Post string: </h3> <label for="lname">Post String:</label><br> <input type="text" id="poststring" name="poststring" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> <input type="submit" value="Submit" style="background-color: black; border: 1px dashed #39FF14; color: #39FF14;"> </form> Ok. Basically, i want the code to output the site info using POST functions and PHP PDO. For some reason though this code is not working for me. I really don't know where I'm going wrong. Can you spot my bug? ATM it will display nothing but "$keyword". Edited July 20, 2022 by oz11 new title Quote Link to comment https://forums.phpfreaks.com/topic/315068-sql-like-keyword-search-not-functioning-as-it-should/ Share on other sites More sharing options...
Solution mac_gyver Posted July 20, 2022 Solution Share Posted July 20, 2022 (edited) the ? place-holder that's put into the sql query statement is only the ? character, no single-quotes, no wild-card characters. any wild-card characters go around the value you are supplying in the execute() call. you should also be using a method = 'get' form when performing a search and to get the form to submit to the same page it is on, simply leave the entire action='...' attribute out of the form tag. also, Don't Repeat Yourself (DRY.) use one single search form. to select which type of search, use either a select/option menu or radio buttons. the serach form should be 'sticky' and repopulate the form fields with any existing search inputs and use css instead of in-line styling. Edited July 20, 2022 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/315068-sql-like-keyword-search-not-functioning-as-it-should/#findComment-1598429 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.