Jump to content

Archived

This topic is now archived and is closed to further replies.

russia5

multi-part form

Recommended Posts

Hello...  Please and Thx for any help you guys might have for me.  I have a multi-part form that feeds to a MySQL   The form seems to work okay, successfully delivering the text data of part 1 of the form to the table as well as the image urls which are on parts 2 thru 5 of the form,  however, I am getting this error on submission of each part of the form:

[b]Warning[/b] Cannot modify header information - headers already sent by (output started at /home/myaccountname/public_html/config.php:28) in /[b]home/myaccountname/public_html/formhandler.php[/b] on line [b]57[/b]

(see underscore below for line 57)

Here is the first part of the form handler:

<?php
if (empty($_REQUEST['step'])) $step = 1; else $step = $_REQUEST['step'];
if (empty($_POST['id'])) $id = 0; else $id = $_POST['id'];
include_once ("config.php");
if (!empty($_POST))
{
if ($step < 3) // insert/update info
{
$fields = $values = array();
unset($_POST['Submit']);
if (empty($_POST['id']))
{
unset($_POST['id']);
foreach ($_POST as $field=>$value)
{
$fields[] = $field;
$values[] = '"'.htmlspecialchars(trim($value)).'"';
}
$query = 'INSERT INTO Profile_submission ('.implode(',', $fields).') VALUES ('.implode(',',$values).')';
mysql_query($query);
$id = mysql_insert_id();
# set cookies
if (!empty($id)) setcookie('authcode', $id, time() + 3600*24*365, '/');
}

[u]Where line 57 is:[/u]
if (!empty($id)) setcookie('authcode', $id, time() + 3600*24*365, '/');

[u]The rest of the formhandler is:[/u]

}
else
{
$qryString = array();
$currentID = $_POST['id'];
unset($_POST['id']);
foreach ($_POST as $field=>$value)
{
$qryString[] = $field.' = "'.htmlspecialchars(trim($value)).'" ';
}
$query = 'UPDATE Profile_submission SET '.implode(',', $qryString).' WHERE sid = "'.$currentID.'"';
mysql_query($query);
}
}
else // upload photos
{
$uploaded_file ="";
// move uploaded file
if ($_FILES['picture']['tmp_name'] != "none" and $_FILES['picture']['tmp_name'] != "")
{
$tmpname = rand(time()-10000, time()).".jpg";
$uploaded_file = 'uploads/'.$tmpname;
if (@move_uploaded_file($_FILES['picture']['tmp_name'], $uploaded_file))
{
chmod($uploaded_file, 0777);
}
}
$query = 'UPDATE Profile_submission SET picture'.($step-2).' = "'.$uploaded_file.'" WHERE sid = '.$id;
mysql_query($query);
}
}
elseif (!empty($_COOKIE['authcode']))
{
$query = 'SELECT * FROM Profile_submission WHERE sid = "'.$_COOKIE['authcode'].'"';
$result = mysql_query($query);
if (mysql_num_rows($result))
{
$profile = mysql_fetch_assoc($result);
$id = $_COOKIE['authcode'];
}
}
if ($step > 6) {
header("Location: ");
}

This form use to work right without this error.  I don't believe a change was made in the form or the handler, however, I could be wrong.

Again, thanks for the help on this problem as well as all the help you have delivered to me in the past.  russia5








Share this post


Link to post
Share on other sites
You MUST setcookie(); before you load ANY information onto the page, or you will get this error.

Please read the sticky in thread forum about "headers sent" before posting issues about headers.

Share this post


Link to post
Share on other sites
Thanks for your helpful response.  After reading the thread you suggested and a couple more, I tryed some things but they did not work.  Let me ask, when you setcookie() you are suppose to set it within the present formhandler script that it is in now, right?  Second, I am not exactly sure where the data is considered loaded.  Would an appropriate spot to setcookie() be below the beginning tag  ie) <?php setcookie() ...  My second guess, is right after <include config.php>

Thanks...


Share this post


Link to post
Share on other sites
header fucntions, session functions and cookie functions must be used before the headers are being sent.
What does this mean? That means that you got to use these functions before there is any output (using functions like echo() will output something).

[code]//Invalid example:
<?php
echo("Something");
setcookie("1","1","1"); //will give a: "Headers already sent" error.
?>


//Valid example:
<?php
setcookie("1","1","1");
echo("Something");
?>
[/code]


Orio.

Share this post


Link to post
Share on other sites
That's what I would guess it to be.  However, for the life of me, I do not see where I am outputting before setcookies()  Line 57  Are my challenged eyes not picking something up that is apparrent to you?

Share this post


Link to post
Share on other sites
Then you must have either:
1) A whitespace or any char before your opening "<?php"
2) Something printed/echoed in in config.php
3) A whitespace or any char before your opening "<?php" or your closing "?>" in config.php

Orio.

Share this post


Link to post
Share on other sites
Very impressive knowledge!  Thankyou very much!  White space is defined, from my understanding, as space that is unoccupied by text within an HTML document.  Since, the form handler is not in an HTML doc and niether is config.php, what is white space outside of php tags and how does one get rid of it?

Share this post


Link to post
Share on other sites
A white space, like a regular space between words, but you just cant see it.
Anyway, there's probbly a space hiding in this file or the config.php before the opening <?php or after the closing "?>".

Orio.

Share this post


Link to post
Share on other sites
Thankyou very much!  I got it.  White space was on the bottom of my config.php  I very much appreciate your help!

Share this post


Link to post
Share on other sites

×

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.