usman07 Posted April 16, 2012 Share Posted April 16, 2012 I have a search form and have set up php, but at the moment its not making the correct searches, what am i doing wrong. Help is appreciated. heres my php code: <?php $server = ""; // Enter your MYSQL server name/address between quotes $username = ""; // Your MYSQL username between quotes $password = ""; // Your MYSQL password between quotes $database = ""; // Your MYSQL database between quotes $con = mysql_connect($server, $username, $password); // Connect to the database if(!$con) { die('Could not connect: ' . mysql_error()); } // If connection failed, stop and display error mysql_select_db($database, $con); // Select database to use // Query database $result = mysql_query("SELECT * FROM Properties"); // Set up our error check and result check array $error = array(); $results = array(); // First check if a form was submitted. // Since this is a search we will use $_GET if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); // remove any html/javascript. if (strlen($searchTerms) < 10) { $error[] = "Search terms must be longer than 10 characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection. } // If there are no errors, lets get the search going. if (count($error) < 1) { $searchSQL = "SELECT sid, sbody, stitle, sdescription FROM simple_search WHERE "; // grab the search types. $types = array(); $types[] = isset($_GET['images'])?"`simages` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['Location'])?"`sLocation` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['Number of bedrooms'])?"`snumberofbedrooms` LIKE '%{$searchTermDB}%'":''; $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked) if (count($types) < 1) $types[] = "`simages` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked $andOr = isset($_GET['matchall'])?'AND':'OR'; $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `sLocation`"; // order by title. $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}"); if (mysql_num_rows($searchResult) < 1) { $error[] = "The search term provided {$searchTerms} yielded no results."; }else { $results = array(); // the result array $i = 1; while ($row = mysql_fetch_assoc($searchResult)) { $results[] = "{$i}: {$row['sLocation']}<br />{$row['sNumberofbedrooms']}<br />{$row['sbody']}<br /><br />"; $i++; } } } } function removeEmpty($var) { return (!empty($var)); } if (!$result) { echo "Error running query:<br>"; trigger_error(mysql_error()); } elseif(!mysql_num_rows($result)) { // no records found by query. echo "No records found"; } else { $i = 0; echo '<div class="container" style="float:left;">'; while($row = mysql_fetch_array($result)) { // Loop through results $i++; echo '<div class="imageholder" style="float:left;">'; echo '<img class="image1" src="'. $row['images'] .'" />'; //image echo '</div>'; echo '<div class="textholder" style="font-family:helvetica; font-size:13px; float:left; padding-top:10px;">'; echo "<span style=\"color:green;\"><b>Displaying record $i<br>\n</b><br></span>"; echo "<b>" . $row['id'] . "</b><br>\n"; // Where 'id' is the column/field title in the database echo "Location: ". $row['Location'] . "<br>\n"; // Where 'location' is the column/field title in the database echo "Property Type: ". $row['Property_type'] . "<br>\n"; // as above echo "Bedrooms: ". $row['Number_of_bedrooms'] . "<br>\n"; // .. echo "Purchase Type: ". $row['Purchase_type'] . "<br>\n"; // .. echo "Price: ". $row['Price_range'] . "<br>\n"; // .. echo '</div>'; echo '<div style="clear:both"></div>'; } echo '</div>'; } mysql_close($con); // Close the connection to the database after results, not before. ?> Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/ Share on other sites More sharing options...
Muddy_Funster Posted April 17, 2012 Share Posted April 17, 2012 so what exactly is the problem? what are you geting back V's what are you expecting back? Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338020 Share on other sites More sharing options...
usman07 Posted April 17, 2012 Author Share Posted April 17, 2012 At the moment im getting no error messages which is good im guessing, but its not searching correctly, for example il press search and it will just display the results, but when I fill in the form I want the form to output depending on what the search is? The website is live here : http://www.mumtazproperties.hostei.com/ The form is on that page^ Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338192 Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2012 Share Posted April 18, 2012 ok, couple of questions: 1. can I get eyes on your database tables +column names and types 2. how upset would you be if I re-wrote your whole code 3. is this actualy going to be a commercial launch or just a personal project Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338346 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 Thanks mate, yeah I can screenshot my MYSQL database and I would not be upset at all if u re-wrote my code, if anything id be absolutely excited! and its more of a personal project at the moment but may become commercial in the future. Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338354 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 Image of my database Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338355 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 Image of my property image database Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338356 Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2012 Share Posted April 18, 2012 ok, first thing you need to do is add name="sensibleName" to every one of your form elements (fyi <select> is an element, <option> is an atribute of <select>, so don't name any options) Then we're going to redisign your database, because you should have more than 1 table. Then we're going to tweek your index page (make it .php if it isn't already, and if it is let me see the code for it) and after all that we can move onto making the searches do what you want them to do. let us know when you have the elements named Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338367 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 ok mate, u mean the form elements in my HTML code? that sets up the form? could you give me an idea of a form element cuz i dont know what one is, thanks. I think i get it cuz u did actually tell me what a element is in the above post, sorry. so its like these: <select multiple="multiple" <select name="property type" Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338377 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 and yeah my index page is HTML not PHP here the HTML form code: <form action="insert.php" method="get"> <table id="tb1"> <tr> <td><p class="LOC">Location:</p></td> <td><div id="LC"> <select multiple="multiple" size="5" style="width: 150px;" > <option>Armley</option> <option>Chapel Allerton</option> <option>Harehills</option> <option>Headingley</option> <option>Hyde Park</option> <option>Moortown</option> <option>Roundhay</option> </select> </div> </td><td><p class="PT">Property type:</p></td> <td><div id="PS"> <select name="property type" style="width: 170px;"> <option value="none" selected="selected">Any</option> <option value="Houses">Houses</option> <option value="Flats / Apartments">Flats / Apartments</option> </select> </div> </td><td> <div id="ptype"> <input type="radio" class="styled" name="ptype" value="forsale"/> For Sale <p class="increase"> <input type="radio" class="styled" name="ptype" value="forrent"/> To Rent </p> <p class="increase"> <input type="radio" class="styled" name="ptype" value="any"/> Any </p> </div> </td> </tr> </table> <div id="table2"> <table id="NBtable"> <tr> <td><p class="NBS">Number of bedrooms:</p></td> <td><div id="NB"> <select name="number of bedrooms"> <option value="none" selected="selected">No Min</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> to <select name="number of bedrooms"> <option value="none" selected="selected">No Max</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </div> </td> <td><p class="PR">Price range:</p></td> <td><div id="PR"> <select name="price range"> <option value="none" selected="selected">No Min</option> <option value="50,000">50,000</option> <option value="60,000">60,000</option> <option value="70,000">70,000</option> <option value="80,000">80,000</option> <option value="90,000">90,000</option> <option value="100,000">100,000</option> <option value="110,000">110,000</option> <option value="120,000">120,000</option> <option value="130,000">130,000</option> <option value="140,000">140,000</option> <option value="150,000">150,000</option> <option value="160,000">160,000</option> <option value="170,000">170,000</option> <option value="180,000">180,000</option> <option value="190,000">190,000</option> <option value="200,000">200,000</option> <option value="210,000">210,000</option> <option value="220,000">220,000</option> <option value="230,000">230,000</option> <option value="240,000">240,000</option> <option value="250,000">250,000</option> <option value="260,000">260,000</option> <option value="270,000">270,000</option> <option value="280,000">280,000</option> <option value="290,000">290,000</option> <option value="300,000">300,000</option> <option value="310,000">310,000</option> <option value="320,000">320,000</option> <option value="330,000">330,000</option> <option value="340,000">340,000</option> <option value="350,000">350,000</option> </select> to <select name="price range"> <option value="none" selected="selected">No Max</option> <option value="50,000">50,000</option> <option value="60,000">60,000</option> <option value="70,000">70,000</option> <option value="80,000">80,000</option> <option value="90,000">90,000</option> <option value="100,000">100,000</option> <option value="110,000">110,000</option> <option value="120,000">120,000</option> <option value="130,000">130,000</option> <option value="140,000">140,000</option> <option value="150,000">150,000</option> <option value="160,000">160,000</option> <option value="170,000">170,000</option> <option value="180,000">180,000</option> <option value="190,000">190,000</option> <option value="200,000">200,000</option> <option value="210,000">210,000</option> <option value="220,000">220,000</option> <option value="230,000">230,000</option> <option value="240,000">240,000</option> <option value="250,000">250,000</option> <option value="260,000">260,000</option> <option value="270,000">270,000</option> <option value="280,000">280,000</option> <option value="290,000">290,000</option> <option value="300,000">300,000</option> <option value="310,000">310,000</option> <option value="320,000">320,000</option> <option value="330,000">330,000</option> <option value="340,000">340,000</option> <option value="350,000">350,000</option> </select> </div> </td> </tr> </table> <div id="submit1"><input type="submit" value="submit" /></div> </div> </form> Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338379 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 thanks so much for your help!iv added more sensible names in the form elements. are these ok: <select multiple="locations" <select name="property type" <select name="min bedrooms"> <select name="max bedrooms"> <select name="min price"> <select name="max price"> Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338382 Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2012 Share Posted April 18, 2012 still need a name on the <select multimple="multiple name="area" should do Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338390 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 iv changed 'location' to 'area' <select multiple="area" are the others ok? Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338391 Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2012 Share Posted April 18, 2012 OK, multiple="multiple" should stay, add in name="area" as well as that, the multiple="multiple" makes it so you can select multiple entrys from the list. next step - better database table structure: CREATE TABLE `catagorys` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `cat_name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE `images` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `property_id` bigint(20) NOT NULL, `image_path` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE `locations` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `area_name` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE `property` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `location_id` bigint(20) DEFAULT NULL, `catagory_id` bigint(20) DEFAULT NULL, `type` varchar(255) DEFAULT NULL, `bedrooms` int(11) DEFAULT NULL, `bathrooms` int(11) DEFAULT NULL, `receptions` int(11) DEFAULT NULL, `parking` varchar(255) DEFAULT NULL, `garden` varchar(255) DEFAULT NULL, `market_type` varchar(255) DEFAULT NULL, `asking_price` decimal(13,2) DEFAULT NULL, `pay_interval` varchar(255) DEFAULT NULL, `summary` varchar(255) DEFAULT NULL, `full_description` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; run that against your database using the Query tab of whatever it is that you access it with (I'm assuming phpMyAdmin as I didn't take a close look at the images) Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338395 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 ahh right like this: <select multiple="multiple" name="area" Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338399 Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2012 Share Posted April 18, 2012 yip, like that Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338401 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 do i run that in here? and yeah im using phpmyadmin Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338402 Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2012 Share Posted April 18, 2012 yeah, just copy and past into that box and run it. then you'll naeed to take some time and look over the tables, make sure you can see what's going on before we move on. Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338405 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 I pasted the code and submitted the query but this came up and it says 'You have to choose at least one column to display' Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338407 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 Iv got it, was meant to query it in the SQL tab. im just looking over the tables now. Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338416 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 yeah the tables are looking good, does it not matter what the Collation is? cuz its set too 'latin1_swedish_ci' Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338418 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 Just a few questions about the field names: What would go in "category_id" "market_type" "pay_interval" thanks Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338420 Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2012 Share Posted April 18, 2012 not hugely important, but you should set it to your local colation, you should be able to change that with the settings in phpMyAdmin. I just havn't set anything on this computer yet because I only use it for inital dev work. So you get how all the tables are going to interact and hold the info for each property listing? category_id -- is going to be the id from the category table that represents the area name that the property is in. market_type -- is going to hold eather Sale, Rent, Rent To Buy that sort of thing pay_interval -- if for rental properties and will be things like 4 - weekly, per calander month, weekly, etc. making sense so far? Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338423 Share on other sites More sharing options...
usman07 Posted April 18, 2012 Author Share Posted April 18, 2012 yeah just 1 more thing, what would "type" field be used for? would it be used for type of property e.g. semi-detached etc. yeah i'm understanding everything thanks, all down to how well u explain things, so thanks! Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338427 Share on other sites More sharing options...
Muddy_Funster Posted April 18, 2012 Share Posted April 18, 2012 type was actualy to hold house or flat, for the search to filter on. There's nothing to search against type of house (yet!) - so we don't need a field for that information - but don't worry, we can cover that another time. anything else about the tables you want to cover or are we ready to start editing index.php? p.s. I'm pretty sure I suck at explaining things, that's why I keep checking Quote Link to comment https://forums.phpfreaks.com/topic/261048-php-search-form/#findComment-1338430 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.