Pepe Perez
Pepe Perez

Reputation: 121

MySQL, PHP Using LIKE Syntax?

I'm trying to make a user search with the following code:

<?php
session_start();
include("../BD/bd.php");

$searched_for = $_POST['searched_for'];
$query = @mysql_query("SELECT * FROM user_media WHERE nombre LIKE '%$searched_for%'") or die(mysql_error());

while($got_users = @mysql_fetch_array($query)){
    echo '<div class="searched-content-info">'.
         '<div class="searched-photo"><img src="'.$got_users['foto'].'"></div>
         <div class="searched-names"><h3>'.$got_users['nombre'].'</h3></div>
         <div class="searched-dates"><h3>'.'Miembro desde: '.$got_users['created_on'].'</h3></div>  
         </div> 
         <div class="divisor-search-user"></div>';
}

?>

But I'm getting all the rows, I just want to display the searched users info, seems like the $query is receiving a clean $searched_for

Any help here? Btw, I'm a little newbie here, please don't bully :)

EDIT: I tried changing $got_users['nombre']; with $searched_for to see if $searched_for is empty and yes it doesn't return any string that's why I am getting all the rows. $query is getting an empty variable but Why?

Here's my HTML:

<form target="u-n" id="search_input" action="search_user.php" method="post">
    <input id="search-input" name="searched_for" type="search" placeholder="Search">
</form>

Upvotes: 0

Views: 99

Answers (1)

Raptor
Raptor

Reputation: 54212

You used <input type="search" /> which is a HTML5 feature. Older browsers may not support this. Replace this input with type="text".

Then, your $_POST['searched_for'] should populate properly, that is:

<input name="searched_for" type="text" placeholder="Search" />

Also, you used the same id multiple times, which is an invalid HTML syntax.

Reference: HTML input tag at MDN

Upvotes: 1

Related Questions