This question has already been answered:

Good afternoon, I will try to paint the problem in as much detail as possible. I have a table. Apartment with fields (rooms, floor, area, price, city). I do a sample of the database on the page

echo GetNav($p, $num_pages); $sel = "SELECT * FROM `kvartiri` LIMIT ".$start.", ".$num_elements; $query = mysql_query($sel); if(mysql_num_rows($query)>0){ ?> <table border="1" cellpadding="15"> <tr> <th>ID</th> <th>Login</th> <th>Email</th> </tr> <?php while($res = mysql_fetch_array($query)){ echo '<tr><td>'.$res['avtor'].'</td>'; echo '<td>'.$res['telephon'].'</td>'; echo '<td>'.$res['etaj'].'</td></tr>'; } ?> </table> 

There is a form on the page. On form input'y. Number of rooms, price, city and FIND BUTTON. I think you understand what I mean. So, how can I shovel the request. I understand with several conditions, but I actually have a problem, and if one of the values ​​is not entered, for example, the fields are filled with the number of rooms, the price, and the city remained empty. Then in where gorod=$gorod would be incorrect.

How to solve a problem?

Reported as a duplicate by Ipatiev php Feb 1 at 7:33 am

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    There is absolutely no problem. Before forming the request, check which of the fields that came from the form are not empty and add them to the request, for example:

     $sql = 'SELECT * FROM `kvartiri` WHERE 1 '; // получаем данные с формы (цена, город) $price = $_POST['price']; $city = $_POST['city']; // если цена не пустая, включаем ее как условие в запрос if (!empty($price)) { $sql .= "AND `price` = '{$price}' "; } // если город не пустой, включаем его как условие в запрос if (!empty($city)) { $sql .= "AND `city` = '{$city}' "; } $sql .= "LIMIT {$start}, {$limit}"; $query = mysql_query($sql); ...