•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 402,373 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,101 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 6946 | Replies: 4
![]() |
| |
•
•
Join Date: Jun 2004
Posts: 11
Reputation:
Rep Power: 5
Solved Threads: 0
I have a form in my search.php page and want to know how to search on more than one field in my Mysql Database to give me the result.
This is my form for searching one field:
<form action="results.php" method="post">
Search: <select name="searchtype">
<option value="date">Date</option>
<option value="wk no.">Wk #</option>
<option value="year">Year</option>
<option value="artist">Artist</option>
<option value="title">Title</option>
</select> <input name="searchterm" type="text">
<INPUT type=image src="search.gif" value="Search"></div>
</form>
The thing is I wish to search on both the wk no. field and theyear field to give the result I want.
The think is that the Year is on multiple records (about 50 records) the Wk No. is the same on only 10 records in the database then the Wk No. changes with the Year staying the same.
This means that if I can search on both the Year and Wk No. it would give me the 10 records that match both criteria.
If I search on just the Year it gives me all 50 records for the year regardless of Wk No.
Or if I search on Wk No. it gives me all the records with the same WK No. in all the different years.
So if anyone can please amend my form code to do this it would be very much appreciated.
This is my form for searching one field:
<form action="results.php" method="post">
Search: <select name="searchtype">
<option value="date">Date</option>
<option value="wk no.">Wk #</option>
<option value="year">Year</option>
<option value="artist">Artist</option>
<option value="title">Title</option>
</select> <input name="searchterm" type="text">
<INPUT type=image src="search.gif" value="Search"></div>
</form>
The thing is I wish to search on both the wk no. field and theyear field to give the result I want.
The think is that the Year is on multiple records (about 50 records) the Wk No. is the same on only 10 records in the database then the Wk No. changes with the Year staying the same.
This means that if I can search on both the Year and Wk No. it would give me the 10 records that match both criteria.
If I search on just the Year it gives me all 50 records for the year regardless of Wk No.
Or if I search on Wk No. it gives me all the records with the same WK No. in all the different years.
So if anyone can please amend my form code to do this it would be very much appreciated.
Last edited by charter : Jun 19th, 2004 at 3:44 pm. Reason: email notification
•
•
Join Date: Mar 2004
Location: In a house
Posts: 94
Reputation:
Rep Power: 5
Solved Threads: 0
The SQL would look something like this:
"SELECT * FROM table WHERE wkno LIKE '%".$_POST['searchterm']."%' OR year LIKE '%".$_POST['searchterm']."%'"
•
•
Join Date: Jun 2004
Posts: 11
Reputation:
Rep Power: 5
Solved Threads: 0
•
•
•
•
Originally Posted by Ragnarok
The SQL would look something like this:
"SELECT * FROM table WHERE wkno LIKE '%".$_POST['searchterm']."%' OR year LIKE '%".$_POST['searchterm']."%'"
Sorry but this isn't what I am looking for.
I want a page with 2 search boxes (the code I gave was for 1 box).
The data base includes 2 fields 1 for wk no and 1 for year.
I want to search in 1 box for the wk no and to search in the other box for the year.
Then for the result to match the wk no. and the year.
This means having 2 search box forms on one page but one button to search for the criteria
•
•
Join Date: Jun 2004
Posts: 11
Reputation:
Rep Power: 5
Solved Threads: 0
Thanks Ragnarok but i'm a newbie to PHP/MySQL
Here's my code for the results page:
This is the full code I have on the results.php 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 `uk` WHERE `{$searchtype}` LIKE '%{$searchterm}%'");
?>
</head>
<body background="0000ff">
<div align="center">
<table class="table" width="760" height="266" border="0" cellpadding="5" cellspacing="0">
<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['tw'] = stripslashes($row['tw']);
$item['lw'] = stripslashes($row['lw']);
// now we can override that
$item['lw'] = ($item['lw']=='new')
? "<img src=\"images/new2.gif\" alt=\"new!\" />"
: $item['lw'];
$item['wks'] = stripslashes($row['wks']);
$item['title'] = strtoupper(stripslashes($row['title']));
$item['artist'] = stripslashes($row['artist']);
?>
<tr>
<td width="28" class="tw"><?php echo $item['tw']; ?></td>
<td width="53" class="lw"><?php echo $item['lw']; ?></td>
<td width="28" class="wks"><?php echo $item['wks']; ?></td>
<td width="340" class="title"><?php echo $item['title']; ?></td>
<td width="307" class="artist"><?php echo $item['artist']; ?></td>
</tr>
<?php $i++; } ?>
</table> </td>
</tr>
</table>
What I want is a AND situation. I wish to enter data for 2 fields into 2 search boxes on the page (field 1 is wk no and field 2 is year) I want the result to find the record (rows) which match both wk no AND year
I also need to know how to set up the search terms on the forms.
Many Thanks
Here's my code for the results page:
This is the full code I have on the results.php 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 `uk` WHERE `{$searchtype}` LIKE '%{$searchterm}%'");
?>
</head>
<body background="0000ff">
<div align="center">
<table class="table" width="760" height="266" border="0" cellpadding="5" cellspacing="0">
<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['tw'] = stripslashes($row['tw']);
$item['lw'] = stripslashes($row['lw']);
// now we can override that
$item['lw'] = ($item['lw']=='new')
? "<img src=\"images/new2.gif\" alt=\"new!\" />"
: $item['lw'];
$item['wks'] = stripslashes($row['wks']);
$item['title'] = strtoupper(stripslashes($row['title']));
$item['artist'] = stripslashes($row['artist']);
?>
<tr>
<td width="28" class="tw"><?php echo $item['tw']; ?></td>
<td width="53" class="lw"><?php echo $item['lw']; ?></td>
<td width="28" class="wks"><?php echo $item['wks']; ?></td>
<td width="340" class="title"><?php echo $item['title']; ?></td>
<td width="307" class="artist"><?php echo $item['artist']; ?></td>
</tr>
<?php $i++; } ?>
</table> </td>
</tr>
</table>
What I want is a AND situation. I wish to enter data for 2 fields into 2 search boxes on the page (field 1 is wk no and field 2 is year) I want the result to find the record (rows) which match both wk no AND year
I also need to know how to set up the search terms on the forms.
Many Thanks
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Searching multiple fields... (PHP)
- Problem searching multiple sheets in excel (Visual Basic 4 / 5 / 6)
Other Threads in the PHP Forum
- Previous Thread: case and image help please
- Next Thread: I need help with a parse error!


Hybrid Mode