Jump to content

"SQL LIKE Keyword" search not functioning as it should


oz11
Go to solution Solved by mac_gyver,

Recommended Posts

<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 by oz11
new title
Link to comment
Share on other sites

  • oz11 changed the title to "SQL LIKE Keyword" search not functioning as it should
  • Solution

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 by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.