Page 1 of 1

getting data from database according to the user selection

Posted: Fri Apr 26, 2013 12:43 am
by shalumike
Hi,

I am trying to do a search. but not getting how exactly i can achieve this.

Here is my html form

Code: Select all

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form1">
Select a color <select name="color">
<option value=""></option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="white">White</option>
</select>
<br /><br />

Select cloth <select name="cloth">
<option value=""></option>
<option value="silk">Silk</option>
<option value="cotton">Cotton</option>
</select>
<br /><br />

Select brand <select name="brand">
<option value=""></option>
<option value="levis">Levis</option>
<option value="ethnica">Ethnica</option>
<option value="biba">Biba</option>
</select>
<br /><br />

Select type <select name="type">
<option value=""></option>
<option value="shirt">Shirt</option>
<option value="skirt">Skirt</option>
<option value="salwar">Salwar</option>
<option value="kurtis">Kurtis</option>
</select>
<input type="submit" value="go" name="go" />
</form>
and my php code is

Code: Select all

<?php

if(isset($_POST['go']))
{

$colors = mysql_real_escape_string($_POST['color']);
$cloths = mysql_real_escape_string($_POST['cloth']);
$brands = mysql_real_escape_string($_POST['brand']);
$type = mysql_real_escape_string($_POST['type']);

include('connect.php');
$sql = "select * from clothing where color = '$colors' OR cloth = '$cloths' OR brand = '$brands' OR type = '$type'";

$result = mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_array($result))
{
//$cloth = $row['cloth'];
//$type = $row['type'];
//$brand = $row['brand'];
//$color = $row['color'];
 echo "<table width='65%' cellpadding='4' border='0'>" ;
              echo "<tr><td>  Cloth:   </td> <td>Type</td> <td>Brand</td><td> Color</td></tr> ";
               echo "<tr><td> " . $row['cloth'] . "</td> <td>" . $row['type'] . "</td><td> " . $row['brand'] . "</td><td> " . $row['color'] . "</td> </tr> </table>";

}


}

?>
attached is the database i am using for this.

now it displays the result which satisfies at least one condition. but i want to display the result when all the selected data matches. if i select only color and type, it should display the data which has both the features in it.

for example: if i select color as blue and cloth as silk, then it should display only three rows of result which has both the two features, but now it is displaying all the rows which has either color as blue and cloth as silk. That shouldn't happen. That am not getting how to do.

please suggest me how to do it.

Re: getting data from database according to the user selecti

Posted: Fri Apr 26, 2013 1:04 pm
by litebearer
Consider the difference between "OR" and "AND" in your query.

Re: getting data from database according to the user selecti

Posted: Fri Apr 26, 2013 3:11 pm
by califdon

Code: Select all

$sql = "select * from clothing where color = '$colors' AND cloth = '$cloths' AND brand = '$brands' AND type = '$type'";