-1

I am trying to find matching results to my query in my database and put it in the table form I made, however I get no matching results back. I ran my query in phpmyadmin and got the desired output. I used var_dump on my $result variable and get the following: resource(4) of type (mysql result). here is my code:

<?php
function renderSearchForm($search, $search_by, $search_by, $error){
?>
<!DOCTYPE HTML5>
<head>
    <title>Search Query</title>
</head>
<body>
<?php
//display errors
    if($error != ''){
        echo '<div style="padding:4px; border:1px solid red; color:red;">' . $error . '</div>';
    }
?>

<form action="" method="post">
<div>
<p>Search For:</p>
<strong>Name: *</strong> <input type="text" name="search" value="<?php echo $search; ?>" /><br/>
<p>Search by:</p>
<input type="radio" name="search_by"
<?php if (isset($search_by) && $search_by=="firstname") echo "checked";?>
value="firstname"/>Firstname*
<input type="radio" name="search_by"
<?php if (isset($search_by) && $search_by=="surname") echo "checked";?>
value="surname"/>Surname*
<br><br>
<input type="submit" name="submit" value="Submit">
<p>* required</p>
</div>
</form>
</body>
</html>
<?php
}

//connect to db
include('connect_db.php');

//check if submitted
if (isset($_POST['submit'])) {
    $search = mysql_real_escape_string(htmlspecialchars($_POST['search']));
    $search_by = mysql_real_escape_string(htmlspecialchars($_POST['search_by']));

    //check if search is empty
    if (empty($_POST["search"])) {
         $error = "Error: Name is required";

         //error, display form
         renderSearchForm($search, $search_by, $search_by, $error);
         } 

         elseif
         // check if name only contains letters and whitespace
          (!preg_match("/^[a-zA-Z ]*$/",$search)) {
           $error = "Error: Only letters and white space allowed";

         //error, display form
         renderSearchForm($search, $search_by, $search_by, $error);
         }

         //check if radio button selected
         elseif (empty($_POST["search_by"])) {
         $error = "Error: Search_by is required";

         //error, display form
         renderSearchForm($search, $search_by, $search_by, $error);
         }else{
            //save data
            $query =  "SELECT * FROM members WHERE '$search_by' LIKE '%$search%'";
            $result = mysql_query($query)
            or die(mysql_error());
            var_dump($result);

            //display data from db
            echo "<table border='1' cellpadding='10'>";
            echo "<tr> <th>ID</th> <th>Firstname</th> <th>Surname</th> <th>Telephone</th> <th>Cell</th> <th>Address</th> <th></th> <th></th> </tr>";

            //loop through results of db and display in table
            while ($row = mysql_fetch_array($result)) {
                //echo contents in table
                echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['firstname'] . "</td>";
                echo "<td>" . $row['surname'] . "</td>";
                echo "<td>" . $row['telephone'] . "</td>";
                echo "<td>" . $row['cellphone'] . "</td>";
                echo "<td>" . $row['address'] . "</td>";
                echo "<td><a href='edit.php?id=" . $row['id'] . "'>Edit</a></td>";
                echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>";
                echo "</tr>";
             }

             echo "</table>";

             //This counts the number or results
             $anymatches=mysql_num_rows($result);  
             if ($anymatches == 0)  {  
             echo "Sorry, but we can not find an entry to match your query<br><br>";  
             }
             //And we remind them what they searched for
             echo "<b>Searched For:</b> " .$search. "<b> In: </b>" .$search_by;   
             }

}else{
    //if form not submitted, display form
    renderSearchForm('','','','');
}
?>

1 Answers1

0

You need to remove the single quotes between $search_by. Try this:

"SELECT * FROM members WHERE $search_by LIKE '%$search%'"
Jean de Lavarene
  • 3,461
  • 1
  • 20
  • 28