Searching glitch (mysql database)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drumking88
Forum Newbie
Posts: 9
Joined: Tue Apr 28, 2009 4:32 pm

Searching glitch (mysql database)

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Searching glitch (mysql database)

Post 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.
drumking88
Forum Newbie
Posts: 9
Joined: Tue Apr 28, 2009 4:32 pm

Re: Searching glitch (mysql database)

Post by drumking88 »

Got it working!

Beautiful percentage variable!
Post Reply