mikebyrne Posted April 3, 2008 Share Posted April 3, 2008 I have a serchfild on my page and want the results to be on my searchresults.php page My code for login.php is <label for="searchfield" class="t10nw"></label><br /><br /><br /><br /><br /><br /> <?PHP $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM product WHERE Producttype = '$cat' AND Productname LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { $product = $row['product']; echo "$product was found<br>"; } } else { echo "No search results found"; } ?> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="cd" selected="selected" >CDs</option> <option value="dvd" >DVDs</option> <option value="game" >Games</option> </select> <form name="ex2" method="get" action="searchresults.php"> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" value="Search Now! »" id="gosrch" /> </td></tr> </form> </table> Searchresults.php <?php print_r($_GET); echo $query; echo $product; echo "Title ".$row['ProductName']."\n<br> <img src=\"".$row['Image']."\"></img>\n<br> Amount in stock ".$row['Stockamount']."\n<br> Price ".$row['Price']."\n<br> Description: ".$row['Description']."\n<br>"; ?> The sql for my product table is CREATE TABLE `product` ( `Producttype` varchar(4) collate latin1_general_ci default NULL, `ProductName` varchar(80) collate latin1_general_ci default NULL, `ProductNo` double(50,0) NOT NULL auto_increment, `Stockamount` decimal(5,0) default NULL, `Display` varchar(3) collate latin1_general_ci default NULL, `Description` varchar(1000) collate latin1_general_ci default NULL, `Price` decimal(6,2) default NULL, `Image` varchar(50) collate latin1_general_ci default NULL, `Imgae2` varchar(50) collate latin1_general_ci default NULL, PRIMARY KEY (`ProductNo`) ) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; If I type Duffy into the searchfield I get the output Array ( [searchfield] => Duffy ) Title Amount in stock Price Description: As you can see the varibles dont seem to have passed Quote Link to comment Share on other sites More sharing options...
bryan52803 Posted April 3, 2008 Share Posted April 3, 2008 Of course it won't. You're getting the search results on the login page which makes no sense. The only variable you're submitting to the following page is the search query. The mysql retrieval and such should be on the searchresults.php page. Login.php should have the form, which should submit to searchresults.php Searchresults.php should: $results = $_POST['searchresults']; THEN do the mysql query on that page and display the results appropriately. What you're doing now makes no sense. Bryan Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2008 Author Share Posted April 3, 2008 I've changed login.php to: <input type="submit" name="searchme" value="Search Now! »" id="gosrch" /> searchresults <?PHP if (isset($_POST['searchme'])) { $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM product WHERE Producttype = '$cat' AND Productname LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { echo "Title ".$row['ProductName']."\n<br> <img src=\"".$row['Image']."\"></img>\n<br> Amount in stock ".$row['Stockamount']."\n<br> Price ".$row['Price']."\n<br> Description: ".$row['Description']."\n<br>"; } } else { echo "No search results found"; } } else { echo "No search terms entered."; } ?> The problem now is im getting the error "No search terms entered." whatever I type into the searchfield Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2008 Author Share Posted April 3, 2008 if fixed this line but still getting the error $input = mysql_real_escape_string($_POST['searchfield']); Quote Link to comment Share on other sites More sharing options...
craygo Posted April 3, 2008 Share Posted April 3, 2008 problem is you started your form after your input fields. your form should start before and input variables. <form name="ex2" method="get" action="searchresults.php"> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="cd" selected="selected" >CDs</option> <option value="dvd" >DVDs</option> <option value="game" >Games</option> </select> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" value="Search Now! »" id="gosrch" /> </td></tr> </form> Also in your form you are using GET so $cat = mysql_real_escape_string($_POST['cat']); is not going to work for you. You will have to use $cat = mysql_real_escape_string($_GET['cat']); Ray Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2008 Author Share Posted April 3, 2008 Thats great! My image files arent getting displayed though???? Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted April 3, 2008 Author Share Posted April 3, 2008 Problem solve. It was pointing to the wrong folder Quote Link to comment 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.