Skip to content Skip to sidebar Skip to footer

My Search Results Aren't Consistent? Why Is This?

I am practicing with php mysql search results. The only problem I am getting is that the search isn't consistent in returning results. Please visit: weezy.co.uk/newresults.php type

Solution 1:

When your page loads with results, you also load a "Tags" sidebar with the question "Want the recruiters to find you?" The input boxes in that sidebar are filled in with "Enter name" and "Enter chosen industry." The "name" attribute on both those inputs is "search," so you are posting multiple fields named "search."

Just change the names on the inputs in your sidebar to fix this bug. You should definitely sanitize the inputs (as others have noted), too.

Solution 2:

I am not sure if it will solve your problem but you should construct the SQL query in this way:

$sql = 'SELECT name,
      lastname,
      email, 
   FROM test_mysql
   WHERE name LIKE %' . mysql_real_escape_string($_POST['search']) . '%AND lastname LIKE %' . mysql_real_escape_string($_POST['searchterm']) . '%';

If you would like to prevent displaying all rows in case user will not fill input values, use something like this:

functionmakeWhereLike($column, $inputValue)
{
   $wherePart = '';
   if ($inputValue) {
      $wherePart = $column . ' LIKE %' . mysql_real_escape_string($inputValue) . '% ';
   } else {
      $wherePart = $column . ' IS NULL ';
   }
   return$wherePart;
}

$sql = '
   SELECT name,
      lastname,
      email, 
   FROM test_mysql
   WHERE ' . makeWhereLike('name', $_POST['search']) . '
      AND ' . makeWhereLike('lastname', $_POST['searchterm']) . '
';

Solution 3:

I'm quite sure there is something wicked in the part of the JS code where you submit this form. I suggest you try to submit it by adding a traditional submit-button there and see what happens.

Every second time you're query echoes "No rows found, nothing to print so am exiting", although it's not visible in results because it's printed in #ffffff.

Post a Comment for "My Search Results Aren't Consistent? Why Is This?"