Forum Moderators: coopster
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!
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 :)
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!
$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