Forum Moderators: coopster

Message Too Old, No Replies

Cookie help!

cant get my cookie to work.

         

1derwoman

5:05 am on Oct 6, 2005 (gmt 0)

10+ Year Member



I have been trying to create a search form that gets data from the database and displays it on screen. IU have that working, but i also need to show the last searched item using a cookie. I cant seem to get the cookie to show my value... im really stumped does anyone know what im doign wrong?

below is my code, id really appreciate some help!


<?
if($action=="main"¦¦!$action)
{
$cookie = $_cookie['testcookie'];
echo"

<html>

<head>
<title>PHP MySQL Example</title>

</head>

<body>

your last search was $cookie

<form name=\"searchform\" action=\"$PHP_SELF\" method=\"post\">
<input name=\"action\" type=\"hidden\" value=\"search\">
<input type=\"text\" name=\"keyword\">
<input type=\"submit\" value=\"search\">
";
}

if($action=="search")
{

$host='localhost';
$userid = '*********;
$password = '********';
$dbname = '********';

$query = "SELECT
ProductNo, ProductName, StyleNo, Size, Fur, Colour, ProductPrice, ProductDesc
FROM Teddies
WHERE (ProductName LIKE '%$keyword%') OR (ProductDesc LIKE '%$keyword%')";

// connect to mysql
$dbc = @mysql_connect($host, $userid, $password) OR
die ('Cannot connect to MySQL '.mysql_error());

//connect to the database
@mysql_select_db($dbname) OR
die ('Cannot connect to database '.mysql_error());

//execute the query
$result = @mysql_query($query) OR
die ('Cannot execute the query '.mysql_error());
setcookie("testcookie", $var1 );

while($row=mysql_fetch_array($result)){
$var1=$row['ProductName'];

echo "<h2>Search Results</h2>";
echo "<table width=80% border=0>";
echo "<tr><td>".$row['ProductName']."</a>
</td></tr><tr><td>".$row['ProductDesc']."</td></tr></table>";

}

mysql_free_result($result);
mysql_close();
}
?>

I hope someone can help!

grandpa

5:22 am on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi 1derwoman. Welcome to Webmasterworld.

It's early yet (still on my first cup of coffee) but it looks like you're trying to set the cookie with a value that you don't yet have. From this code:

//execute the query
$result = @mysql_query($query) OR
die ('Cannot execute the query '.mysql_error());
setcookie("testcookie", $var1 );

What is in $var1 at this point? Also, if this query fails, everything comes to a screeching halt because of your use of or die...

Since it appears that you are setting $var1 in your while loop, then you'll want to set the cookie value after that.

while($row=mysql_fetch_array($result)){
$var1=$row['ProductName'];
echo "<h2>Search Results</h2>";
echo "<table width=80% border=0>";
echo "<tr><td>".$row['ProductName']."</a>
</td></tr><tr><td>".$row['ProductDesc']."</td></tr></table>";
}
setcookie("testcookie", $var1 );

The only problem here is that if your query returns more than a single result, only the last result will be set in the cookie. If your query is intended to only produce a single row, it might be better to use mysql_fetch_row instead of mysql_fetch_array.

Disclaimer: Besides being early I've noticed that lately I haven't really understood the question, so if I'm off base here just wait until someone with a grasp of reality happens along :)

1derwoman

7:06 am on Oct 6, 2005 (gmt 0)

10+ Year Member



You input is muchly appreciated! I have no clue so anything is possible!

I had thought about setting the cookie afterwards but then i just get

Warning: Cannot modify header information - headers already sent by....

As far as i know you have to set the cookie before anything is sent. Its kinda confusing, so that unforunatly doesnt work. But thanks anyways!

adb64

10:38 am on Oct 6, 2005 (gmt 0)

10+ Year Member



Indeed, the cookie must be sent before any page data. In such cases I first collect all the output in a variable, e.g.:

$Pagedata .= <whatever you want to write in the page>;
$Pagedata .= <more page output>;

iso

echo <whatever you want to write in the page>;
echo <more page output>;

And then, after you've set the cookie:

echo $Pagedata;

Another possibility is using output buffering [php.net], see example 1 there.

Regards,
Arjan