Jump to content

When nothing is inputted to a field an error occurs


frank_solo

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.