Jump to content

Passing vairbles between pages


mikebyrne

Recommended Posts

At present ive coded a serchfield on my login page which should pass the results to searchresults.php. I presume i need to use the $_GET [Result];

 

I'm just not sure how to code searchresults.php to see whats getting passed over

 

login.php

 

<table width="100%" cellpadding="0" cellspacing="0" border="0" class="b1sw2" style="background:url(Pictures/searchbox_bg.gif) no-repeat #003;">
<col width="1%">
<col width="99%">

<tr>
<td> </td>
<td>
<label for="searchfield" class="t10nw"></label><br /><br /><br /><br /><br /><br />
<?PHP
include("adminconnect.php");
$tbl_name = "product";

$cat = mysql_real_escape_string($_POST['cat']);
$input =
mysql_real_escape_string($_POST['searchfield']);

$query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'";
$result = mysql_query($result);
if (mysql_num_rows($result) > 0) {
     while($row = mysql_fetch_array) {
         $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_cd.php">

<input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" />
<input type="submit" value="Search Now! »" id="gosrch" />

</td></tr>
</form>
</table>

Link to comment
Share on other sites

what I normally do to find out exactly what is being passed across and how is to comment out all the code on your searchresults.php and then echo out all the contents of the array

 

print_r($_GET);
//or if you use post
print_r($_POST);

 

is that what you meant?

Link to comment
Share on other sites

When login.php loads i get the error

 

arning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\MainPage\login.php on line 136

No search results found

 

Line 136 is:

 

if (mysql_num_rows($result) > 0) {

Link to comment
Share on other sites

your problem lies in this bit of code here

<?php
$query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'";
$result = mysql_query($result);
if (mysql_num_rows($result) > 0) {
?>

 

as you can see in the second line of the three above you say $result is to equal mysql_query(itself), if you would have had an error catcher on this then you would have found out your problem. try the following code, note the change and also the error catch statement put it

 

 

<?php
$query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'";
$result = mysql_query($query) or die ("error in the query" . mysql_error());
if (mysql_num_rows($result) > 0) {
?>

 

and if you want to echo out the whole of the $_GET then all you need is

 

<?php
print_r($_GET);
?>

 

 

hope that helps

Link to comment
Share on other sites

Im getting the error

 

error in the queryYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%%'' at line 1

 

<?PHP
include("adminconnect.php");
$tbl_name = "product";

$cat = mysql_real_escape_string($_POST['cat']);
$input =
mysql_real_escape_string($_POST['searchfield']);

$query = "SELECT * FROM $tbl_name WHERE $cat 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) {
         $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" >CD</option>
<option value="dvd" >DVD</option>
<option value="game" >Game</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>

 

Link to comment
Share on other sites

try this

$query = "SELECT * FROM $tbl_name WHERE '$cat' LIKE '%$input%'"; //you missed off the single quotes from $cat

 

if it still does not function after that then check to see whether $cat and $input are being set properly by echoing them out

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.