Forum Moderators: coopster

Message Too Old, No Replies

Cookie problem

         

Numberman

2:05 am on Mar 20, 2006 (gmt 0)

10+ Year Member



Hey,

I'm working on a script that uses cookies extensively, however, I've run into a problem.

It's split into two sections.

This is a form. It pulls data from a DB for each item.

function stage5()
{
// Skills
$query = 'SELECT name FROM tblskill';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo("<table width='200' border='0' cellspacing='0' cellpadding='0'>");
echo("<tr>");
echo("<td>Skill</td>");
echo("<td>Ranks</td>");
$skillno = 1;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
foreach ($line as $colvalue)
{
$uccolvalue = ucwords($colvalue);
echo("<tr>");
echo("<td>");
echo($uccolvalue);
echo("</td>");
echo("<td>");
echo("<input name='$skillno' type='text' size='7' maxlength='2'>");
echo("</td>");
echo("</tr>");
$skillno++;
}
}
echo("<input name='totalskills' type='hidden' value='$skillno'>");
echo("</table>");
}

The posted data is processed here:

function stage5()
{
// Skills
$skillno = $_POST['totalskills'];
$no = 1;
while ($no <= $skillno)
{
$cookie = array("skill", $no);
setcookie(implode("",$cookie), $_POST[$no++]);
}
}

Basically, I've got a table full of values. I'm trying to generate a form that shows each one, and allows a number to be entered beside it. When processed, the data entered should be saved as a cookie, with a reference number.

However, when I run the page, I get an error:

Notice: Undefined offset: 36 in c:\program files\easyphp1-8\www\process.php on line 96

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\process.php:96) in c:\program files\easyphp1-8\www\process.php on line 96
Please wait... Storing data.

As far as I can see, it's telling me that it can't send the same cookie twice. However, I thought my code would have each cookie saved have a unique ID (in the scope of the script). What's going wrong?

Any help appreciated.

Mr_Fern

3:04 am on Mar 20, 2006 (gmt 0)

10+ Year Member



It's trying to access some array at the index 36
$array[36], but there's no value at $array[36] and since error reporting is on E_ALL, Notices (generally uninitialized variable reports) are included, and a notice is generated. The notice sends data to the screen. cookies are like header values, can't be edited once data's printed out.

You could change the error reporting to exclude notices, you could do output buffering, or you can make sure the script doesn't reference $array[36]