I explode the input of user to array and then search them in the database but if user enter the space as a result it will show the whole rows of the table which has space how can I make it correct?
if(isset($_POST['submit'])){
$keywords = explode(" ", $_POST["search"]);
for ($i=0; $i<count($keywords); $i++) {
$query = "SELECT * FROM mp3s " .
"WHERE (artist LIKE '%".$keywords[$i]."%'
OR genre LIKE '%".$keywords[$i]."%'
OR album LIKE '%".$keywords[$i]."%'
OR filename LIKE '%".$keywords[$i]."%'
) ";
$sql = mysql_query($query) or die(mysql_error());
}
Use
trim()to delete spaces and usemysql_real_escape_string()to prevent sql injections.But it’s better to use
MySQLithan themysql_real_escape_string()function.See http://php.net/manual/en/function.mysql-real-escape-string.php
Or PDO with the prepared statements :
http://php.net/manual/en/pdo.prepared-statements.php