Jump to content

Displaying results on next page


mikebyrne

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

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.