0

i'm creating a search function with a drop down list where the user can search based on the chosen drop down list and display it on the same page. When I press the submit button, it didn't display any result.

Sorry if this has been asked before, but I suspect there is something that I did wrong at the PHP code but I could not figure out why. Can someone please point it out for me?

This is my search form:

<h2>Search</h2>
<form name="search" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  Keyword &nbsp;
  <input type="text" name="searchq" id="searchq" size="30" placeholder="Enter keyword..">&nbsp; By &nbsp;
  <select name="searchopt" id="searchopt">
    <option value=""></option>
    <option value="Members">Members</option>
    <option value="Journal">Journal</option>
    <option value="Conference">Conference</option>
    <option value="Awards">Awards</option>
    <option value="Grants">Grants</option>
    <option value="Patents">Patents</option>
    <option value="Research Grants">Research Grants</option>
    <option value="Book Chapter">Book Chapter</option>
    <option value="Book Publications">Book Publications</option>
    <option value="Other Publications">Other Publications</option>
  </select>
  &nbsp;
  <input type="submit" id="submit" name="submit" value="Search">
</form>
<br>
<p>
  <h4>Results</h4>
</p>

And this is my PHP code:

include ("includes/dbcon.php");

if(isset($_POST['searchq']) && $_POST['searchq'] != "")
{
    if(isset($_POST['searchopt']) && $_POST['searchopt'] != "")
    {
        $escsearch = mysqli_real_escape_string($conn, $_POST['searchq']);
        $searchval = preg_replace('#[^a-z 0-9]#i', '', $escsearch);
        $opt = $_POST['searchopt'];

        switch($opt)
        {
            case "Members":
            $query = "SELECT * FROM members where memberName LIKE '%$searchval%'";
            $res = mysqli_query($conn, $query) or die(mysqli_error());
            $count = mysqli_num_rows($res);
            if($count > 1)
            {
                $output .= "$count results for <strong>$escsearch</strong>.";

                while($row = mysqli_fetch_array($res))
                {
                    $name = $row['memberName'];
                    $pos = $row['position'];
                    $fac = $row['faculty'];
                    $email = $row['email'];
                    $int = $row['research_interests'];

                    echo "<table>
                            <tr>
                                <td>Name</td>
                                <td>Position</td>
                                <td>Faculty</td>
                                <td>E-mail</td>
                                <td>Research Interests</td>
                            </tr>
                            <tr>
                                <td>$name</td>
                                <td>$pos</td>
                                <td>$fac</td>
                                <td>$email</td>
                                <td>$int</td>
                            </tr>
                         </table>";
                }
            }else{
                $output = "<p>No records found.</p>"; 
            }
            break;
        }
    }
}
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
Azie
  • 27
  • 1
  • 2
  • 8

3 Answers3

1

Numerous problems here:

  1. Your submit button should not have a name attribute since you are not doing anything with it.

    Change this:

    <input type="submit" id="submit" name="submit" value="Search">
    

    To this:

    <input type="submit" id="submit" value="Search">
    
  2. your if conditional, checking for results will return false if one result is found.

    Change this:

    if($count > 1)
    

    To this:

    if($count > 0)
    
  3. Lastly, your $output variable is defined the wrong way. Using .= assumes that $output has been previously defined.

    Change this:

    $output .= "$count results for <strong>$escsearch</strong>.";
    

    To this:

    $output = "$count results for <strong>$escsearch</strong>.";
    
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
0

Correct you condition

if($count > 1) => if($count >= 1)

It seems like you have only one record in database

Also define $output variable

Vishu238
  • 673
  • 4
  • 17
0

If there is only one record with membername LIKE lau then you will get nothing output to the page.

This line says there has to be more than one result for the script to continue... if($count > 1) You might want it to be if($count >= 1)

First you have to define $output as an empty string... $output = '';

Hope this helps

Kuya
  • 7,280
  • 4
  • 19
  • 31