Jump to content

[SOLVED] Php MySQL Database Search Problem !!


nita

Recommended Posts

Hi.

I'm building little script to be used with my movie collection.

Basically everything worked fine so far. Recently i added Search option to the script!

Problem is :

When  you search from the root is fine (index.php), but once you try to do so while you browsing on of the categories is not returning search results, is printing movies from this category again, instead.

Seems like there is a problem with passing the information from search to browser while browsing categories.

Take a look at the code:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK REL="StyleSheet" href="movies.css" TYPE="text/css">

<title>Nita Movies</title>
</head>

<body>

<?
include "connectdb.php";

if(isset($_GET['cat'])) // when looking at selected category of movies
{
echo "
<table width='950'>
<tr>
<td bgcolor='#663300' width='180' border='0' cellspacing='0' cellpadding='0'  valign='top' class='font2' >";
$result = mysql_query("SELECT * FROM category ORDER BY cat") or die(mysql_error());
while($row = mysql_fetch_array( $result )) 
{
echo "<A href='index.php?cat=$row[cat]'>$row[cat]</a><br>";
}
echo "	
<br>
<form method='post' action='$php_self'>
<input type='text' name='search' size=25 maxlength=25><br>
<input type='Submit' name='Submit' value='Submit'>
</form>
</td>

<td>";
$cat=$_GET['cat'];
	$row2=mysql_query("SELECT * FROM movies WHERE cat='$cat'");
	while($row=mysql_fetch_array($row2))
	{
include "display_rec.php";
}
	echo "
</td>
</tr>
</table>";
}

else if(isset($_POST['submit'])||isset($_POST['search'])) // when search button is pressed
{
echo "
<table width='950'>
<tr>
<td bgcolor='#663300' width='180' border='0' cellspacing='0' cellpadding='0'  valign='top' class='font2' >";
$result = mysql_query("SELECT * FROM category ORDER BY cat") or die(mysql_error());
while($row = mysql_fetch_array( $result )) 
{
echo "<A href='index.php?cat=$row[cat]'>$row[cat]</a><br>";
}
echo "	
<br>
<form method='post' action='$php_self'>
<input type='text' name='search' size=25 maxlength=25><br>
<input type='Submit' name='Submit' value='Submit'>
</form>
</td>

<td>";

$search=$_POST["search"];

$result = mysql_query("SELECT * FROM movies WHERE name LIKE '%$search%' 
OR cast LIKE '%$search%' OR director LIKE '%$search%' 
OR year LIKE '%$search%' OR production LIKE '%$search%'
OR genere LIKE '%$search%' OR language LIKE '%$search%'");
$numrows=mysql_num_rows($result);

if ($numrows == 0)
  	{
  	echo "<h4></h4>";
  	echo "<p>Sorry, your search returned 0 results</p>";
  	}
else 
{ 
while($row=mysql_fetch_array($result))
{	
   	include "display_rec.php";
}
}
	echo "
</td>
</tr>
</table>";

}

else  //when looking at the root of the aplication

{
echo "
<table>
<tr>
<td bgcolor='#663300' width='180' border='0' cellspacing='0' cellpadding='0' valign='top' class='font2' >	";
$result = mysql_query("SELECT * FROM category ORDER BY cat") or die(mysql_error());
while($row = mysql_fetch_array( $result )) 
{
echo "<A href='index.php?cat=$row[cat]'>$row[cat]</a><br>";
}
echo "	
<br>
<form method='post' action='$php_self'>
<input type='text' name='search' size=25 maxlength=25><br>
<input type='Submit' name='Submit' value='Submit'>
</form>
</td>
<td>
Some Introduction!
</td>
</tr>
</table>";
}

?>

</body>
</html>

 

If some one can help me to get this to work, will be great - so i can move on.

?? if else construction maybe??

 

There is another think i would like to ask for help with too.

what should be done to the search script so it want display results if search will be no longer then 2 letters / numbers ??

 

you can also see this script in action at:

www.nita-on-line.com/movies/

 

Thanks a lot in advance !!!

 

Nita

nita.travel@ekit.com

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.