PDA

Click to See Complete Forum and Search --> : search


stickman373
Aug 14th, 2002, 01:43 PM
$result = mysql_query("SELECT * FROM movie WHERE title LIKE $_REQUEST['search'] ORDER by title DESC");

if ($record = mysql_fetch_array($result)) {
do {
echo $record['title'] . "<br>\n";
} while ($record = mysql_fetch_array($result));
} else {
echo "No records found!";
}


I'm trying to use the code above but it doesn't work, so what is wrong? I have a submit button that posts to this script and a textbox called search. So when i hit submit it should post the contents of search to this script then the script displays the records which match the query.

what is wrong with my code?


Also I have a combo box with different colums that i can search by. How can i have it so when the info is posted, the text in the combo box called cats is posted and then depending on what is in the combo box it changes the query?

cpradio
Aug 14th, 2002, 01:44 PM
$result = mysql_query("SELECT * FROM movie WHERE title LIKE '%".$_REQUEST['search']."%' ORDER by title DESC");

if ($record = mysql_fetch_array($result)) {
do {
echo $record['title'] . "<br>\n";
} while ($record = mysql_fetch_array($result));
} else {
echo "No records found!";
}

stickman373
Aug 14th, 2002, 01:55 PM
thanks, do u happen to know how to do the second part?

like i have a combo box caled cats with the following in it: Title, Date, Author

Now when i his search if the combo box says Title it does this query:

"SELECT * FROM movie WHERE title LIKE '%".$_REQUEST['search']."%' ORDER by title DESC"

if it says Date it does this query:

"SELECT * FROM movie WHERE date LIKE '%".$_REQUEST['search']."%' ORDER by title DESC"

and if it says author it does this query:

"SELECT * FROM movie WHERE author LIKE '%".$_REQUEST['search']."%' ORDER by title DESC"

How would i get the script to choose the right query based on $_REQUEST['cats']

?

cpradio
Aug 14th, 2002, 02:02 PM
This should be your combo box:
<select name="cats" size="1">
<option value="title">Title</option>
<option value="date">Date</option>
<option value="author">Author</option>
</select>


Your PHP Code:
$result = mysql_query("SELECT * FROM movie WHERE ".$_REQUEST['cats']." LIKE '%".$_REQUEST['search']."%' ORDER by title DESC");

if ($record = mysql_fetch_array($result)) {
do {
echo $record['title'] . "<br>\n";
} while ($record = mysql_fetch_array($result));
} else {
echo "No records found!";
}

stickman373
Aug 14th, 2002, 02:13 PM
thanks a bunch :D