spearchilduser Posted February 28, 2012 Share Posted February 28, 2012 Hi guys and girls Right im not directly asking for code but i am aware how to set up and make a single search in a form against a database the issue ive come across is that i was to be able for the user to search against 3 different columns in the table each with their own textbox and submit button just wonderign if i would do this on 3 different forms or if it is easy ? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/257918-search/ Share on other sites More sharing options...
scootstah Posted February 28, 2012 Share Posted February 28, 2012 Without knowing more I'd say you'd probably want 3 forms. Quote Link to comment https://forums.phpfreaks.com/topic/257918-search/#findComment-1321990 Share on other sites More sharing options...
spearchilduser Posted February 28, 2012 Author Share Posted February 28, 2012 Ok i basiclaly have a list of houses each with attributes price street name area etc I want the user to be able to search againt price street name area etc all on one page it can be with one submit button or seperate ones jsut wonderign hwo i woudl check which textboxes have information in them or search using information in all of them ? Quote Link to comment https://forums.phpfreaks.com/topic/257918-search/#findComment-1321991 Share on other sites More sharing options...
scootstah Posted February 28, 2012 Share Posted February 28, 2012 In that case, you would only need one form. You could do something like if (!empty($_POST)) { $price = $_POST['price']; $street = $_POST['street']; $area = $_POST['area']; $where = array(); if (!empty($price)) { $where[] = "price LIKE '%$price%'"; } if (!empty($street)) { $where[] = "street LIKE '%$street%'"; } if (!empty($area)) { $where[] = "area LIKE '%$area%'"; } $where = implode(' OR ', $where); $result = mysql_query('SELECT * FROM houses WHERE ' . $where); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { echo 'results...<br />'; } } else { echo 'no matches!'; } } ?> <form action="" method="post"> Price: <input type="text" name="price" /><br /> Street: <input type="text" name="street" /><br /> Area: <input type="text" name="area" /><br /> <input type="submit" name="submit" value="Search" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/257918-search/#findComment-1321993 Share on other sites More sharing options...
spearchilduser Posted February 28, 2012 Author Share Posted February 28, 2012 Hi thanx for that so atm ive got the following: <?php session_start(); include("db.php"); check_valid_user(); mysql_connect("localhost", "root") or die(mysql_error()); // makes a connection mysql_select_db("Computers1") or die('I cannot connect to the database because: ' . mysql_error()); //conects to the database if (!empty($_POST)) { $Price = $_POST['Price']; $Model = $_POST['Model']; $Storage = $_POST['Storage']; $where = array(); if (!empty($Price)) { $where[] = "price LIKE '%$Price%'"; } if (!empty($Model)) { $where[] = "Model LIKE '%$Model%'"; } if (!empty($Storage)) { $where[] = "Storage LIKE '%$Storage%'"; } $where = implode(' OR ', $where); $result = mysql_query('SELECT * FROM items WHERE ' . $where); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { echo 'results...<br />'; $id = $row["itemId"]; $Model = $row["Computer_Model"]; $Price= $row["Computer_Price"]; $Storage = $row["Computer_Storage"]; $Processor = $row["Computer_Processor_Name"]; $Monitor = $row["Computer_Monitor_Size"]; $desc= $row["Description"]; echo '<tr><td>'; echo "<h3>$id<h3>"; echo "Price :<strong> $Price </strong><br />"; echo "Model :<strong> $Model</strong><br />"; echo "Storage :<strong> $Storage</strong><br />"; echo "Monitor :<strong> $Monitor</strong><br />"; echo "Processor :<strong> $Processor</strong><br />"; echo "desc :<strong> $desc </strong><br />"; echo '</td></tr>'; } } else { echo 'no matches!'; } } ?> <form action="" method="POST"> Price: <input type="text" name="Price" /><br /> Model: <input type="text" name="Model" /><br /> Storage: <input type="text" name="Storage" /><br /> <input type="submit" name="submit" value="Search" /> </form> Im testing it on a databse of computers until it works fully so i dont mess up the other one seems silly but it works for me The error im now getting is even if the ebtry is a match it says no matches and Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\dancomp\Search.php on line 35 error apears after a search is made. Quote Link to comment https://forums.phpfreaks.com/topic/257918-search/#findComment-1321995 Share on other sites More sharing options...
spearchilduser Posted February 28, 2012 Author Share Posted February 28, 2012 Any help is apreciated Quote Link to comment https://forums.phpfreaks.com/topic/257918-search/#findComment-1321998 Share on other sites More sharing options...
scootstah Posted February 28, 2012 Share Posted February 28, 2012 The query is probably failing due to an error, run this and post back the output: $query = 'SELECT * FROM items WHERE ' . $where; $result = mysql_query($query) or die ('Query: ' . $query . '<br />Error: ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/257918-search/#findComment-1322013 Share on other sites More sharing options...
ManiacDan Posted February 28, 2012 Share Posted February 28, 2012 you need to run all these user-submitted variables through mysql_real_escape_string to avoid SQL injection. Also, I wouldn't search based on price, it seems silly to have a user search for "2" and get back something that costs $82.50. Quote Link to comment https://forums.phpfreaks.com/topic/257918-search/#findComment-1322033 Share on other sites More sharing options...
scootstah Posted February 28, 2012 Share Posted February 28, 2012 Also, I wouldn't search based on price, it seems silly to have a user search for "2" and get back something that costs $82.50. I think a better approach would be to use a "min" and "max" price, and use MySQL's BETWEEN operator. Quote Link to comment https://forums.phpfreaks.com/topic/257918-search/#findComment-1322054 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.