frank_solo Posted March 1, 2012 Share Posted March 1, 2012 I have a form to search through apartments but if one field (rent) is not filled the php script cannot process the search. Is there a way that I can have the script just search the minimum rent or maximum rent if those fields are not filled in? In other words could I make it say if blank put in 0 for minimum rent? The HTML Form: <form method="post" action="searchapts.php"> <table width="372" border="0" class="apttable"> <tr> <td width="66" style="color: #000">Borough:</td> <td colspan="2"><select name="county" id="county"> <option selected="selected">Bronx</option> <option>Brooklyn</option> <option>Manhattan</option> <option>Queens</option> <option>Staten Island</option> <option>---------------</option> <option>Nassau</option> <option>Suffolk</option> </select></td> </tr> <tr> <td style="color: #000">Beds:</td> <td colspan="2"><select name="type" id="type"> <option>0 Bed</option> <option>1 Bed</option> <option>2 Bed</option> <option>3 Bed</option> <option>4 Bed</option> <option>5 Bed</option> </select></td> </tr> <tr> <td style="color: #000">Rent:</td> <td width="151" style="color: #000"><span id="AptRentMin"> <span id="sprytextfield3"> <input name="min_price" type="text" id="min_price" value="" size="7" maxlength="7" /> <span class="textfieldRequiredMsg">Required.</span></span>(Min)</td> <td width="141" style="color: #000"><span id="AptRentMax"> <span id="sprytextfield4"> <input name="max_price" type="text" id="max_price" value="" size="7" maxlength="7" /> <span class="textfieldRequiredMsg">Required.</span></span>(Max)</td> </tr> <tr> <td style="color: #000"><input name="Search" id="Search" value="Search" type="submit" /></td> <td> </td> <td> </td> </tr> </table> </form> The PHP Script: <?php if ($_POST){ $county = $_POST['county']; $rooms = $_POST['type']; $rentmin = $_POST['rent_min']; $rentmax = $_POST['rent_max']; } $dbase = mysql_connect ( 'localhost', '', '' ); mysql_select_db ( '', $dbase ); if($county){ $sql = "SELECT * FROM `apartments`, `county` = '".mysql_real_escape_string($county)."', `rooms` = '".mysql_real_escape_string($rooms)."', `MIN(rent)` '".mysql_real_escape_string($rentmin)."', `MAX(rent)` '".mysql_real_escape_string($rentmax)."' order by `date_created` DESC"; }else{ $sql = "SELECT * FROM `apartments`"; } $res = mysql_query($sql, $dbase); if ( mysql_num_rows($res) > 0 ) { echo "<strong>Click Headers to Sort</strong>"; echo "<table border='0' align='center' bgcolor='#999969' cellpadding='3' bordercolor='#000000' table class='sortable' table id='results'> <tr> <th> Title </th> <th> Borough </th> <th> Town </th> <th> Phone </th> <th> Rooms </th> <th> Bath </th> <th> Fees </th> <th> Rent </th> </tr>"; while($row = mysql_fetch_assoc($res)) { echo "<tr> <td bgcolor='#FFFFFF' style='color: #000' align='center'> <a href='classified/searchapts/index.php?id=".$row['id']."'>" . $row['title'] . "</a></td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['county'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['town'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['phone'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rooms'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['bath'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['feeornofee'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rent'] . "</td> </tr>"; } echo "</table>"; print_r($apts); } else { echo "<p> </p><p> </p> No Results <br /><p> </p><FORM><INPUT TYPE='button' VALUE='Go Back' onClick='history.go(-1);return true;'></FORM> and Refine Your Search <p> </p><p> </p>"; } ?> Link to comment https://forums.phpfreaks.com/topic/258018-when-nothing-is-inputted-to-a-field-an-error-occurs/ Share on other sites More sharing options...
BigTime Posted March 1, 2012 Share Posted March 1, 2012 yes...nest another if and modify the query if($county){ if($rentmin && $rentmax ){ } if ($rentmin && !$rentmax){ } etc.... Link to comment https://forums.phpfreaks.com/topic/258018-when-nothing-is-inputted-to-a-field-an-error-occurs/#findComment-1322569 Share on other sites More sharing options...
frank_solo Posted March 1, 2012 Author Share Posted March 1, 2012 I'm sorry to sound dumb but without doing so much of the work can you explain a little further about if($county){ if($rentmin && $rentmax ){ } if ($rentmin && !$rentmax){ } Do I put a value in "{ }" Or just simply write it like this: if($county){ if($rentmin && $rentmax ){ } if ($rentmin && !$rentmax){ } $sql = "SELECT * FROM `apartments`, `county` = '".mysql_real_escape_string($county)."', `rooms` = '".mysql_real_escape_string($rooms)."', `MIN(rent)` '".mysql_real_escape_string($rentmin)."', `MAX(rent)` '".mysql_real_escape_string($rentmax)."' order by `date_created` DESC"; }else{ $sql = "SELECT * FROM `apartments`"; } Thanks Link to comment https://forums.phpfreaks.com/topic/258018-when-nothing-is-inputted-to-a-field-an-error-occurs/#findComment-1322579 Share on other sites More sharing options...
jcbones Posted March 1, 2012 Share Posted March 1, 2012 Your query syntax is wrong, Consider the following, I commented the changes I made. <?php if ($_SERVER['REQUEST_METHOD'] == 'POST'){ //if data was posted. $dbase = mysql_connect ( 'localhost', '', '' ); //database connection. mysql_select_db ( '', $dbase ); //database selection. if(!empty($_POST['county'])){ //if county is not empty. $county = mysql_real_escape_string($_POST['county']); //escape the string. $query[] = "county = '$county'"; //save the valid column name and value to a array index. } if(!empty($_POST['type'])) { //if type is not empty. $rooms = mysql_real_escape_string($_POST['type']); //escape the string. $query[] = "rooms = '$rooms'"; //save the valid column name and value to an array index. } if(!empty($_POST['rent_min']) && !empty($_POST['rent_max'])) { //if the rent_min AND the rent_max is not emtpy. $min = (int)$_POST['rent_min']; //rent min to an integer. $max = (int)$_POST['rent_max']; //rent max to an integer. $query[] = "(rent BETWEEN $min AND $max)"; //save to a valid column name and check for anything between these two values. } elseif(!emtpy($_POST['rent_min'])) { //if only rent min is filled out. $min = (int)$_POST['rent_min']; $query[] = "rent >= $min"; //just check for a values that are greater than or equal to this value. } elseif(!empty($_POST['rent_max'])) { $max = (int)$_POST['rent_max']; $query[] = "rent <= $max"; //same here opposite way. } } $sql = "SELECT * FROM `apartments` "; //start our query string. if(isset($query) && is_array($query)) { //$query will only be set if there is a POST to the page, $query will only be an array, if something was filled out. $sql .= 'WHERE ' . implode(' AND ',$query); //add a where clause, and populate with the $query array, joining together on ' AND '. } $sql .= "order by `date_created` DESC"; //end the sql with our order by clause. $res = mysql_query($sql, $dbase) or trigger_error($sql . ' has encountered an errror<br /> ' . mysql_error()); //NOTHIGN BELOW THIS LINE WAS CHANGED> if ( mysql_num_rows($res) > 0 ) { echo "<strong>Click Headers to Sort</strong>"; echo "<table border='0' align='center' bgcolor='#999969' cellpadding='3' bordercolor='#000000' table class='sortable' table id='results'> <tr> <th> Title </th> <th> Borough </th> <th> Town </th> <th> Phone </th> <th> Rooms </th> <th> Bath </th> <th> Fees </th> <th> Rent </th> </tr>"; while($row = mysql_fetch_assoc($res)) { echo "<tr> <td bgcolor='#FFFFFF' style='color: #000' align='center'> <a href='classified/searchapts/index.php?id=".$row['id']."'>" . $row['title'] . "</a></td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['county'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['town'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['phone'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rooms'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['bath'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['feeornofee'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rent'] . "</td> </tr>"; } echo "</table>"; print_r($apts); } else { echo "<p> </p><p> </p> No Results <br /><p> </p><FORM><INPUT TYPE='button' VALUE='Go Back' onClick='history.go(-1);return true;'></FORM> and Refine Your Search <p> </p><p> </p>"; } ?> Link to comment https://forums.phpfreaks.com/topic/258018-when-nothing-is-inputted-to-a-field-an-error-occurs/#findComment-1322587 Share on other sites More sharing options...
frank_solo Posted March 1, 2012 Author Share Posted March 1, 2012 Thanks for your help Jcbones but I get this error: Fatal error: Call to undefined function emtpy() on line 12 Link to comment https://forums.phpfreaks.com/topic/258018-when-nothing-is-inputted-to-a-field-an-error-occurs/#findComment-1322596 Share on other sites More sharing options...
jcbones Posted March 1, 2012 Share Posted March 1, 2012 Take a minute and look at it. It is misspelled. EMPTY. Blame it on the fat fingers. Link to comment https://forums.phpfreaks.com/topic/258018-when-nothing-is-inputted-to-a-field-an-error-occurs/#findComment-1322601 Share on other sites More sharing options...
frank_solo Posted March 1, 2012 Author Share Posted March 1, 2012 I have the same problem. Thanks for the help but I now get this error: Notice: SELECT * FROM `apartments` WHERE county = 'Queens' AND rooms = '2' AND rent >= 1000order by `date_created` DESC has encountered an errror You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'by `date_created` DESC' at line 1 on line 287 It happen when I either field is empty Link to comment https://forums.phpfreaks.com/topic/258018-when-nothing-is-inputted-to-a-field-an-error-occurs/#findComment-1322604 Share on other sites More sharing options...
frank_solo Posted March 1, 2012 Author Share Posted March 1, 2012 Actually I found the problem thank you so much for your help! A space had to be added before the function order: $sql .= " order by `date_created` DESC "; Link to comment https://forums.phpfreaks.com/topic/258018-when-nothing-is-inputted-to-a-field-an-error-occurs/#findComment-1322609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.