Help needed with a form!

Discussion in 'PHP' started by karl_murphy, Mar 28, 2009.

  1. #1
    Hi,

    I have created the following simple form:

    
    <?php
    echo "<form method = 'post' action = 'search_results.php'>"; 
    echo "<table>";
    	echo "<tr>";
    		echo "<td>";
    			echo "Property Type";
    		echo "</td>";
    		echo "<td>";
    			echo "<select name = 'type'>";
    				echo "<option value = 'n'>No Preference</option>";
    	
    				$sql = "SELECT DISTINCT `PR_Type` FROM `Properties`";
    				
    				$db = mysql_connect("localhost", "*****", "*****");
    	
    				mysql_select_db('dolphinr_Dolphin',$db);
    				mysql_error();
    				$rst = mysql_query($sql, $db);
    
    				while($row = mysql_fetch_array($rst))
    				{	
    					echo "<option value = '" . $row['PR_Type'] . "'>" . $row['PR_Type'] . ""; 
    				}
    			echo "</select>";
    		echo "</td>";
    	echo "</tr>";
    	echo "<tr>";
    		echo "<td>";
    			echo "Bedrooms";
    		echo "</td>";
    		echo "<td>";
    			echo "<select name = 'bedrooms'>";
    				echo "<option value = 'n'>No Preference</option>";
    				echo "<option value = '1'>1</option>";
    				echo "<option value = '2'>2</option>";
    				echo "<option value = '3'>3</option>";
    				echo "<option value = '4'>4</option>";
    				echo "<option value = '5'>5</option>";
    			echo "</select>";
    		echo "</td>";
    	echo "</tr>";
    	echo "<tr>";
    		echo "<td>";
    			echo "Furnished";
    		echo "</td>";
    		echo "<td>";
    			echo "<select name = 'furnished'>";
    				echo "<option value = 'n'>No Preference</option>";
    				echo "<option value = 'yes'>Yes</option>";
    				echo "<option value = 'no'>No</option>";
    				echo "<option value = 'partial'>Partial</option>";
    			echo "</select>";
    		echo "</td>";
    	echo "</tr>";
    	echo "<tr>";
    		echo "<td>";
    			echo "Rent Type";
    		echo "</td>";
    		echo "<td>";
    			echo "<select name = 'rent'>";
    				echo "<option value = 'n'>Student</option>";
    				echo "<option value = 'r'>Residential</option>";
    			echo "</select>";
    		echo "</td>";
    	echo "</tr>";
    	echo "<tr>";
    		echo "<td>";
    			echo "Price Range";
    		echo "</td>";
    		echo "<td>";
    			echo "<select name = 'price'>";
    				echo "<option value = 'n'>No Preference</option>";
    				echo "<option value = '30'>up to £30 pw</option>";
    				echo "<option value = '40'>up to £40 pw</option>";
    				echo "<option value = '50'>up to £50 pw</option>";
    				echo "<option value = '60'>up to £60 pw</option>";
    				echo "<option value = '70'>up to £70 pw</option>";
    				echo "<option value = '80'>up to £80 pw</option>";
    				echo "<option value = '90'>up to £90 pw</option>";
    				echo "<option value = '100'>up to £100 pw</option>";
    			echo "</select>";
    		echo "</td>";
    	echo "</tr>";
    	echo "<tr>";
    		echo "<td>";
    			echo "<input type = 'submit' value = 'Click to Search'>";
    		echo "</td>";
    	echo "</tr>";
    echo "</form>";
    ?>
    PHP:
    This script works fine and all the selections made through the drop down menus are posted to the search_results script.

    The problem I'm having is with the query within the search_results script. If the user selects "No Preference" in any of the drop down menus how do I represent this in the select query to find the matching data? The reason I'm having difficulty is because no data within the database has the value of "No Preference. If the user selects "No Preference" I want the query to select any value for that field as I cannot simply leave the said field out of the query.

    Any help with this would be greatly appreciated.

    Regards.
     
    karl_murphy, Mar 28, 2009 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Short answer, you don't represent it in the select query. You leave it off.

    If you post the search_results.php code I can help you specifically, but generally this is how it should work.

    First, validate all the incoming data. Don't ever trust it to be what you expect it to be.

    After that you should build your select query, specifically the 'WHERE' portion of it. For every input on the form you will create another part of your 'WHERE' clause. If the value of that data designates 'No Preference' you don't include that portion of the 'WHERE' clause. For example:

    
    if (($_POST['bedrooms']>=1) && ($_POST['bedrooms']<=5)) $sql.=" AND (bedrooms=".$_POST['bedrooms'].")";
    
    PHP:
     
    plog, Mar 28, 2009 IP
  3. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    How would that work exactly?

    The query I am trying to build in the search_results script looks like this:

    $sql = "SELECT * FROM `Properties`
    WHERE `PR_Type` = '" . $type . "'"
    if(bedrooms != 'n') $sql.= "AND `PR_Bedrooms` = '" . $bedrooms . "'";

    I get the following syntax error though:

    Parse error: syntax error, unexpected T_IF in /***/****/****/search_results.php on line 10
     
    karl_murphy, Mar 29, 2009 IP
  4. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $sql = "SELECT * FROM `Properties`
    WHERE `PR_Type` = '" . $type . "'"
    if($bedrooms != 'n') $sql.= "AND `PR_Bedrooms` = '" . $bedrooms . "'";

    you had bedrooms instead of $bedrooms ?
     
    dean5000v, Mar 29, 2009 IP
  5. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Still getting the syntax error even with that SELECT statement.
     
    karl_murphy, Mar 29, 2009 IP
  6. dean5000v

    dean5000v Peon

    Messages:
    201
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    not sure but could be because because you havent used any curly brackes so the conditional statment aint gona work.


    $sql = "SELECT * FROM `Properties`
    WHERE `PR_Type` = '" . $type . "'" ";
    if($bedrooms != 'n') {
    $sql.= "AND `PR_Bedrooms` = '" . $bedrooms . "'"
    };

    try adding .mysql_error() in the statment so you can see what the problem is !
     
    dean5000v, Mar 29, 2009 IP
  7. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $sql = 'SELECT * FROM Properties WHERE PR_Type = "' . $type . '"';
    $sql.=Append($bedrooms,'Bedrooms');

    function Append($Var,$Name)
    {
    if($Var!='n')
    return ' AND `PR_'.$Name.'` = "' . $Var . '"';
    }

    That should REALLY make it easier to append new variables...

    Hit me up on IM or messenger(in my profile) if you need more help.
     
    NatalicWolf, Mar 29, 2009 IP