getting data from database according to the user selection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shalumike
Forum Newbie
Posts: 1
Joined: Fri Apr 26, 2013 12:37 am

getting data from database according to the user selection

Post 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.
Attachments
sql.PNG
sql.PNG (12.08 KiB) Viewed 1246 times
sql.PNG
sql.PNG (12.08 KiB) Viewed 1246 times
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: getting data from database according to the user selecti

Post by litebearer »

Consider the difference between "OR" and "AND" in your query.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: getting data from database according to the user selecti

Post by califdon »

Code: Select all

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