Can any of the gurus out there please help?
I am having a search problem with PHP and MySQL.

I wish to search the MySql database from 2 criteria.
Using 2 dropdown boxes.

I wish to search from 1 dropdown box called searchtype and another called searchterm. The problem is I cannot get them to work do a proper search.
I can get it to work using just one search criteria but I want the result to be of both criteria being true.

This is the form code for the search:

<form action="resultsus50a.php" method="post">
<p>Search:&nbsp;
<select name="searchtype">
<option value="year">2000</option>
<option value="year">2001</option>
<option value="year">2002</option>
<option value="year">2003</option>
<option value="year">2004</option>
</select>
&nbsp;&nbsp;
<p>
<select name="searchterm">
<option value="wk no.">1</option>
<option value="wk no.">2</option>
<option value="wk no.">3</option>
<option value="wk no.">4</option>
<option value="wk no.">5</option>
</select>
&nbsp;&nbsp;


<INPUT type=image src="search.gif" value="Search">
</form>


This is the code for the results page:


<?php
// create short variable names


if(ini_get('register_globals') != "1")
{
$post = (floatval(substr(phpversion(), 0, 3)) >= 4.1) ? $_POST : $HTTP_POST_VARS;


foreach($post as $key => $value)
{
$$key = $value;
}
}
$searchtype = addslashes(trim($searchtype));
$searchterm = addslashes(trim($searchterm));


if(!$searchtype || !$searchterm)
{
exit('You have not entered search details. Please go back and try again.');
}
$link_id = @mysql_connect("localhost", "root", "password");


if($link_id === false)
{
exit("Error, Could not connect to the system database. Sorry...");
}
mysql_select_db('charts50', $link_id);
$result = mysql_query("SELECT * FROM `usa` WHERE `{$searchtype}` LIKE '%{$searchterm}` LIKE %'");
?>
</head>


<body background="images/ground.jpg">
<div align="center">


<table class="table" width="760" height="266" border="0" cellpadding="5" cellspacing="0">
<tr>
<td valign="top"><div align="center"><img src="dateImages/we54-31.gif"></div></td>
</tr>
<tr>
<td height="29" class="years-ago">50 YEARS AGO</td>
</tr>
<tr align="left">
<td height="29" colspan="2" class="years-ago">
<table width="100%" border="0" cellspacing="0" cellpadding="1">
<tr class="headings">
<tr>
<th class="headings" width="28">TW</th>
<th class="headings" width="53">LW</th>
<th class="headings" width="28">Wks</th>
<th class="headings" width="340">Title</th>
<th class="headings" width="307">Artist</th>


</tr>
<?php $i = 1; while($row = mysql_fetch_array($result)){


$item = array();
$item = stripslashes($row);
$item = stripslashes($row);


// now we can override that


$item = ($item=='new')
? "<img src=\"images/new2.gif\" alt=\"new!\" />"
: $item;



$item    = stripslashes($row);
$item  = strtoupper(stripslashes($row));
$item = stripslashes($row);
?>



<tr>
<td width="28" class="tw"><?php echo $item; ?></td>
<td width="53" class="lw"><?php echo $item; ?></td>
<td width="28" class="wks"><?php echo $item; ?></td>
<td width="340" class="title"><?php echo $item; ?></td>
<td width="307" class="artist"><?php echo $item; ?></td>
</tr>
<?php $i++; } ?>

I want the results to be true of both searchtype and searchterm.

If someone could please help by amending the correct code or tell me what I should amend on either the search form or the results code or both. That is true to year and wk no.
I have put 'password' just for this post.

All help is very much appreciated.

Edited 3 Years Ago by Nick Evan: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.