Page 1 of 1

Searching glitch (mysql database)

Posted: Tue Sep 22, 2009 3:23 pm
by drumking88
Ive been trying to create a system where the user can search through a table of tracks by inputing the name of the track. The problem is that the code works for one word songs like "Bonkers" or "Beautiful", but anything more than that it falls to pieces.

Code: Select all

 
 
Search by Track Title!
<form method="post" action="title_request.php">
<input name="user_input" type="text" /><br />
<input type="submit" />
</form>
<?php
 
$input = $_POST['user_input']; //user's requested information passed via form
 
$username = "root";
$password = "";
 
echo $input;
 
$db_link = mysql_connect('localhost', $username, $password); //connection parameters, server, username, and password (set to default). 
if (!$db_link) {
    die('Could not connect: ' . mysql_error()); //if connection doesnt work, error message retrieved and displayed (debugging purposes)
}
if ($db_link)
{
 
    //echo 'Connected successfully <br>'; //statement issued if connection == right!
    $dbselect = mysql_select_db('purefm', $db_link);
    if ($dbselect)
    {   
    
    $input = addslashes(trim($input)); //mysql real escape string thingy deprecated in PHP 6
    $inputarr = explode(' ', $input);
    $inputdb = "('" . implode("','",$inputarr) . "')";
    $result = mysql_query("SELECT Name, Surname, Title FROM jazler_songs WHERE $inputdb LIKE Title");
    
 while($row = mysql_fetch_array($result))
  {
  echo '<form method = "POST" action = "check_request.php">';
  echo '<table border = "0">';
  echo '<tr>';
  echo '<td width = "200">';
 
    $item = $row['Title'];
    $artistFirstName = $row['Name'];
    $artistSurname = $row['Surname'];
    $artistFullName = $artistFirstName. " ".$artistSurname;
 
  echo '<input type = "hidden" name = "song_name" value = " '. $item.' ">';
    echo $item;
  echo '</td>';
  echo '<td width = "400">';
  echo '<input type = "hidden" name = "artist" value = " '.$artistFullName.'">';
  echo $artistFullName;
  echo '</td>';
  /*echo '<td>';
  echo $row['content'];
  echo '</td>';
  */
  echo '<td>';
  
  echo '<input type = "submit" name = "submit" value = "Request It Now ">';
  echo '</td>';
  echo '</tr>';
  echo '</table>';
   echo '</form>';
  } 
  }
  }
  
 
 
 
 
 
 
 
?>
 
I know its something tiny thats causing the glitch, but can't think what it is.

Any ideas would be appreciated.

Re: Searching glitch (mysql database)

Posted: Tue Sep 22, 2009 4:17 pm
by requinix

Code: Select all

$inputdb LIKE Title
They're backwards. You should be checking if the field is LIKE a string.

With multiple strings you have multiple LIKEs.

Re: Searching glitch (mysql database)

Posted: Tue Sep 22, 2009 4:44 pm
by drumking88
Got it working!

Beautiful percentage variable!