View Full Version : Search form help

03-08-2010, 04:04 PM
Hi there I asked about this in another forum however did not receive much helpful response on this.

I have a search form, that is to search for members across my site. see below:

<form method="post" action="quick-search.php">

<div class="form_row">
<label>Instrument group played</label>
<select name="instrument">
<option value = "strings">Strings</option>
<option value = "percussion">Percussion</option>
<option value = "brass">Brass</option>
<option value = "DJ">DJ</option>
<option value = "wind">Wind</option>
<option value = "vocals">Vocals</option>
</select><br />

<label>Looking for</label>
<select name="seeking">

<div class="form_row">
<label>Looking to</label>
<select name="purpose">
<option value="Form a band">Form a band </option>
<option value="Jam">Jam </option>
<option value="Perform">Perform </option>
<option value="One off performance">One off performance </option>
<option value="Regular performance">Regular Performance </option>

<div class="form_row">
<label>Age looking for</label>
<select name="age">
<option value="13">13 </option>
<option value="14">14 </option>
<option value="15">15 </option>
<option value="16">16 </option>
<option value="17">17 </option>
<option value="18">18 </option>
<option value="19">19 </option>
<option value="20">20 </option>
<option value="21">21 </option>
<option value="22">22 </option>
<option value="23">23 </option>
<option value="24">24 </option>
<option value="25">26 </option>
<option value="27">27 </option>
<option value="28">28 </option>
<option value="29">29 </option>
<option value="30">30 </option>
<option value="31">31 </option>
<option value="32">32 </option>
<option value="33">33 </option>
<option value="34">34 </option>
<option value="35">35 </option>
<option value="36">36 </option>
<option value="37">37 </option>
<option value="38">38 </option>
<option value="39">39 </option>
<option value="41">40 </option>
<option value="42">42 </option>
<option value="43">43 </option>
<option value="44">44 </option>
<option value="45">45 </option>

<div class="form_row">
<label>By Postcode</label>
<input class="inputfield" name="pc" type="text" />

<div class="form_row">
<label>By Location</label>
<select name="location">
<option value = "Aberdeenshire">Aberdeenshire</option>
<option value = "Angus">Angus</option>
<option value = "Argyll and Bute">Argyll and Bute</option>
<option value = "Ayrshire">Ayrshire</option>
<option value = "Bedfordshire">Bedfordshire</option>
<option value = "Berkshire">Berkshire</option>
<option value = "Wrexham">Wrexham</option>
<option value = "Buckinghamshire">Buckinghamshire</option>
<option value = "Cambridgeshire">Cambridgeshire</option>
<option value = "Cheshire">Cheshire</option>
<option value = "Clackmannanshire">Clackmannanshire</option>
<option value = "Cornwall">Cornwall</option>
<option value = "Derbyshire">Derbyshire</option>
<option value = "Devon">Devon</option>
<option value = "Dorset">Dorset</option>
<option value = "Dunbartonshire">Dunbartonshire</option>
<option value = "Durham">Durham</option>
<option value = "East Lothian">East Lothian</option>
<option value = "Essex">Essex</option>
<option value = "Fife">Fife</option>


<div class="form_row">
<label>By name of member/band</label>
<input class="inputfield" name="name" type="text" />

<input type="submit" value="Submit" id="Submit" />


Sorry for the long code, but It will make it clearer as to what I want. As seen from the form I have several inputs to search for, however I am not sure how to do the sql statement so that it either searches for one or a few of the search fields.

I was given this code to help me.

$qry = "SELECT * FROM profiles"
if($instrument != '') {$qry .= ($count==1? " WHERE": " AND") . "instrument = '" . $instrument . "' ";$count++;}
if($purpose != '') {$qry .= ($count==1? " WHERE": " AND") . "purpose = '" . $purpose . "'"; $count++;}
if($seeking != '') {$qry .= ($count==1? " WHERE": " AND") . "seeking= '" . $seeking . "'";$count++;}
if($age != '') {$qry .= ($count==1? " WHERE": " AND") . "age = '" . $age . "'";$count++;}

I was told that this code would allow the search to either only take one field or several to return a specific result so for example 'age-20 who plays guitar'.
This example uses wildcard, however my search form has mainly drop down menus, so how can this format be put here?

Also if I try to run this, it errors saying 'unexpected T_IF line 110'....110 being the first IF in this..

Any help would be greatly appreciated.!

03-08-2010, 04:32 PM
missing asemi-colon at the end of this line

$qry = "SELECT * FROM profiles"

03-08-2010, 05:00 PM
Ok thanks, that fixed the error.

But could someone help me with the code? It just returns every single member, regardless of what is searched.

03-08-2010, 05:21 PM
$result = "SELECT * FROM profiles";
if($instrument != '') {$result .= ($count==1? " WHERE": " AND") . "instrument = $instrument ";$count++;}
if($purpose != '') {$result .= ($count==1? " WHERE": " AND") . "purpose =$purpose"; $count++;}
if($seeking != '') {$result .= ($count==1? " WHERE": " AND") . "seeking= '$seeking";$count++;}
if($age != '') {$result .= ($count==1? " WHERE": " AND") . "age = $age ";$count++;}


$num_results = mysql_num_rows($numresults);

I'm getting an error on the &num_results

"Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Users\Ash\Documents\Testing site\xampp\htdocs\upload\quick-search.php on line 118"

I'm not sure why, anyone help?