Jump to content

[SOLVED] Multiple Field Search not Working


kingnutter

Recommended Posts

Hi everyone.

 

This is my first attempt at Search form. It works for a single search term, but my OPTION VALUE 'All' throws up errors (a common problem I can see from Googling, but no solution is working for me).

 

Can anyone tell me where I might be going wrong?

 

Many thanks,

KN

 

<form name="search" method="post" action="index.php">
Search for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="track_artist">Track Artist</option>
<Option VALUE="track_title">Track Title</option>
<Option VALUE="track_title, track_artist">All</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>

<?php

// includes
include('conf.php');
include('functions.php');

// Grab POST data sent from form
$field = $_POST['field'] ;
$find = $_POST['find'] ;
$searching = $_POST['searching'] ;

//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}

// Otherwise we connect to our Database
// open database connection
$connection = mysql_connect($host, $user, $pass)
or die ('unable to connect!');

//select database
mysql_select_db($db) or die ('unable to select database!');



// We preform a bit of filtering

$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

if ($field=='All')

{
$data = mysql_query("SELECT track_artist, track_title FROM tracks WHERE upper(track_artist) LIKE '%$find%' OR upper(track_title) LIKE '%$find%'");
}
else
//Or we search for our single search term, in the field the user specified
{
$data = mysql_query("SELECT track_artist, track_title FROM tracks WHERE upper($field) LIKE'%$find%'");
}

// And we display the results 

while($result = mysql_fetch_array( $data ))
{
echo $result['track_artist'];
echo " ";
echo $result['track_title'];
echo "<br>";
echo "<br>";
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}

else
{
?> 

Duh. Not even a php problem. Sorry guys.

 

I've changed the Option values:

 

<Option VALUE="track_artist">Track Artist</option>
<Option VALUE="track_title">Track Title</option>
<Option VALUE="All">All</option>

 

and the page is just refreshing.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.