Jump to content

Strange result from select list


mozzy

Recommended Posts

Hello,

 

My first (noob) post here.

 

I'm creating a complex search form that seems to work except for the final select list - id ="shape".

 

The form:

 

<form id="searchform_1" name="searchform_1" method="POST" action="results.php">
<tr>
<td height="51" colspan="3"><h2>Advanced search</h2></td>
</tr>
<tr>
<td width="142" height="51">Tree type<br />
</td>
<td colspan="2">
<label>
<input name="evergreen" type="radio" id="treetype_0" value="yes" checked="checked" />
Evergreen</label>

<label>
<input type="radio" name="evergreen" value="no" id="treetype_1" />
Deciduous</label>
<br />
</td>
</tr>
<tr>
<td height="164" valign="top">Tree categories</td>
<td width="269" valign="top" nowrap="nowrap">
<label>
<input type="checkbox" name="autumn" value="yes" id="autumn_check" />
Autumn</label>

<br />
<label>
<input type="checkbox" name="flowering" value="yes" id="flowering_check" />
Flowering/Ornamental</label>
<br />
<label>
<input type="checkbox" name="shadetree" value="yes" id="shade_check" />
Shadetree</label>
<br />
<label>
<input type="checkbox" name="narrowspaces" value="yes" id="narrow_check" />
Narrow spaces</label>

</td>
<td width="322" valign="top" nowrap="nowrap">
<label>
<input type="checkbox" name="hedge" value="yes" id="hedge_check" />
Hedge & screening</label>
<br />
<label>
<input type="checkbox" name="driveways" value="yes" id="driveways_check" />
Driveways & avenues</label>
<br />
<label>
<input type="checkbox" name="street" value="yes" id="street_check" />
Street trees</label>
<br />
<label>
<input type="checkbox" name="drought" value="yes" id="drought_check" />
Drought tolerant</label>
<br />
<label>
<input type="checkbox" name="coastal" value="yes" id="coastal_check" />
Coastal</label>
<br />
<label>
<input type="checkbox" name="pots" value="yes" id="pots_check" />
Courtyard & pots</label>
<br />

</td>
</tr>
<tr>
<td height="64" valign="top" nowrap="nowrap">Tree size</td>
<td colspan="2" align="left" valign="top" nowrap="nowrap" style="font-size: 10px; font-family: Verdana, Geneva, sans-serif;"><select name="size" size="1" id="size">
<option selected="selected">Select a tree size range</option>
<option value="small">Small (1-2 metres)</option>
<option value="medium">Medium (2-10 metres)</option>
<option value="large">Large (> 10 metres)</option>
</select></td>
</tr>
<tr>
<td height="64" valign="top" nowrap="nowrap">Foliage colour</td>
<td colspan="2" align="left" valign="top" nowrap="nowrap" style="font-size: 10px; font-family: Verdana, Geneva, sans-serif;"><select name="colour" size="1" id="colour">
<option selected="selected">Select a colour</option>
<option value="green">Green</option>
<option value="red">Dark Red/Purple</option>
<option value="yellow">Yellow/Gold</option>
<option value="blue">Blue</option>

</select></td>
</tr>
<tr>
<td height="64" valign="top" nowrap="nowrap">Additional criteria</td>
<td align="left" valign="top" nowrap="nowrap" style=""><label>
<input type="checkbox" name="windhardy" value="yes" id="windhardy" />
Wind hardy</label>
<br />
<label>
<input type="checkbox" name="frosthardy" value="yes" id="frosthardy" />
Frost hardy</label>
</td>
<td align="left" valign="top" nowrap="nowrap" style="">
<label>
<input type="checkbox" name="fullsun" value="yes" id="fullsun" />
Full/Part sun</label> <br /> <label>
<input type="checkbox" name="fullshade" value="yes" id="fullshade" />
Full shade</label>
</td>
</tr>
<tr>
<td height="64" valign="top" nowrap="nowrap">Tree form</td>
<td align="left" valign="top" nowrap="nowrap" style=""><select name="shape" size="1" id="shape">
<option vallue ="no">Select tree form</option>
<option value="columner">Columner</option>
<option value="conical">Conical</option>
<option value="hedge">Hedge</option>
<option value="irregular">Irregular</option>
<option value="open">Open</option>
<option value="oval">Oval</option>
<option value="pyramidal">Pyramidal</option>
<option value="round">Round</option>
<option value="shrub">Shrub</option>
<option value="spreading">Spreading</option>
<option value="vase">Vase</option>
<option value="weeping">Weeping</option>
</select></td>
<td align="left" valign="top" nowrap="nowrap" style=""> </td>
</tr>
<tr>
<td height="64" colspan="3" valign="middle" nowrap="nowrap" style="border-top-color: #666; border-top-style: solid; border-top-width: 1px;">Search
<input type="submit" name="newsearch" id="newsearch" value="Submit" />
<input type="reset" name="reset" id="reset" value="Reset" /></td>
</tr></form>

 

The trouble is - if I don't select anything from the last select list the query returns no result. Even though I have by default selected "evergreen" in the top-most radio group.

 

The query:

$Catlist = array();
$where = array();
//.....more code here
// the first two drop downs

//do sizes
if (($_POST['size'] == "small") || ($size == 'small')) {
$where[] = "size = 'small'";
$Catlist[] = "Small tree";
} else
if (($_POST['size'] == "medium") || ($size == 'medium')){
$where[] = "size = 'medium'";
$Catlist[] = "Medium sized tree";
} else
if (($_POST['size'] == "large") || ($size == 'large')){
$where[] = "size = 'large'";
$Catlist[] = "Large tree";
} else
if ($_POST['size'] === "") {
$where[] = "";
$Catlist[] = "";
}

//do colour

if (($_POST['colour'] == "green") || ($colour == 'green')) {
$where[] = "colour = 'green'";
$Catlist[] = "Green foliage";
} else
if (($_POST['colour'] == "red") || ($colour == 'red')){
$where[] = "colour = 'red'";
$Catlist[] = "Dark red/purple foliage";
} else
if (($_POST['colour'] == "yellow") || ($colour == 'yellow')){
$where[] = "colour = 'yellow'";
$Catlist[] = "Yellow/Gold foliage";
} else
if (($_POST['colour'] == "blue") || ($colour == 'blue')){
$where[] = "colour = 'blue'";
$Catlist[] = "Blue foliage";
} else
if ($_POST['colour'] === "") {
$where[] = "";
$Catlist[] = "";
}
//The last drop down
if(isset($_POST['shape']) && ($_POST['shape']) != "") {
$treeform = $_POST['shape'];
$where[] = "treeform = '".$treeform."'";
$Catlist[] = ucwords($treeform)." tree form";
}
//Building my query
$where_size = count($where);
for ($i = 0;$i < $where_size; $i++)
$q_where .= "$where[$i] AND ";
$q_where = substr($q_where,0,-4); // remove the last " OR ";
//PAGINATION
// Number of records found
$sqlCount = "SELECT * FROM all_trees WHERE $q_where ORDER BY tree_common_name";
$resCount = mysql_query($sqlCount);
// Number of results per page
$display = 10;
if(isset($_GET['page'])) {
$currentPage = $_GET['page'];
}else{
$currentPage = 1;
}
//limit in the query thing
$limitQ = 'LIMIT ' .($currentPage - 1) * $display .',' .$display;
$sql = "SELECT * FROM all_trees WHERE $q_where ORDER BY tree_common_name $limitQ";
$res = mysql_query($sql);

 

Basically, I cannot get a proper result if I don't select anything from the Tree shape list - the last drop down.

 

I know some of my methods are probably not ideal but any pointers would be appreciated.

 

cheers

Mozz

Link to comment
https://forums.phpfreaks.com/topic/273773-strange-result-from-select-list/
Share on other sites

First of all, your code is vulnerable to SQL Injection. That's really bad.

 

I'm not sure what's the problem but I will show you two things. Firstly:

 

 

<option vallue="no">Select tree form</option>

 

As you see, there's no attribute like "vallue".

 

Secondly:

 

Input named "evergreen" is defined twice so you're overwritting first input with next one.

as well as the above, your logic checks to see if the value of $_POST['shape'] is set or is not an empty string, this will never fire, as you will always have a value in it, even if it is "no", you need to either change your form value to be "" for the default, or change your logic to check for != "no" or the WHERE is going to include WHERE shape = "no" and I would like to assume that's not going to return any results.

A simple debugging tip. Use this code to see what is posted to your processing script

 

echo '<pre>',print_r($_POST, true),'</pre>';

 

In this case, when you submit a supposedly empty form you would see

 

Array
(
[evergreen] => yes
[size] => Select a tree size range
[colour] => Select a colour
[shape] => Select tree form
[newsearch] => Submit
)

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.