Jump to content

Search DB


djlfreak

Recommended Posts

I have 3 fields in tbl_product that I want to search : pd_dir, pd_cast, pd_name. It's a movie product table and I want users to be able to search the movies by name, director and cast. Not sure of code to display them as it involves getting an image from database. This is what I've tried but of course it's not working. Am I even close?

Search Products Table

$search = (isset($_GET['search'])) ? $_GET['search'] : '';
$sql = 'SELECT
pd_id    FROM        tbl_product
WHERE        
MATCH (pd_name, pd_dir, pd_cast) AGAINST ("' .
mysql_real_escape_string($search, $db) . '" IN BOOLEAN MODE)
ORDER BY        
MATCH (pd_name, pd_dir,pd_cast) AGAINST ("' .


mysql_real_escape_string($search, $db) . '" IN BOOLEAN MODE) DESC';
$result = mysql_query($sql, $db) or die(mysql_error($db));
if (mysql_num_rows($result) == 0) {
echo '<p><strong>No movies found that match the search terms.</strong></p>';
} else {   
while ($row = mysql_fetch_array($result)) {
output_movie($db, $row['pd_id']);    
   }
}
mysql_free_result($result);?>

 

Search Box

<form method="get" action="movie_search.php">   
<div style="padding-left: 25px;">    
<label for="search"><b>Search Movies</b></label>
<br>
<?php	echo '<input type="text" id="search" name="search" ';
if (isset($_GET['keywords'])) {   
echo ' value="' . htmlspecialchars($_GET['keywords']) . '" ';}
echo '/>';
?>
<input type="submit" value="Search" />    
</div>   
</form>

Display a movie from the database.

function output_movie($db, $pd_id) {  
  if (empty($pd_id)) {      
  return;   
}  
  $sql = 'SELECT
pd_name, pd_dir, pd_cast, pd_thumbnail         
FROM  
tbl_product        
WHERE
pd_id = ' . $pd_id;
$result = mysql_query($sql, $db) or die(mysql_error($db));
if ($row = mysql_fetch_assoc($result)) {        
extract($row);
echo '<h2>' . htmlspecialchars($pd_name) . '</h2>';
echo '<p>Dir: ' . htmlspecialchars($pd_dir) . '</p>';
echo '<p>Cast: ' . htmlspecialchars($pd_cast) . '</p>';
echo '$pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail;
} else {           
echo 'No Movies match your search.';      
  }    
}    
mysql_free_result($result);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.