/    Sign up×
Community /Pin to ProfileBookmark

Problem With My Edit Email Script!

I’ve just made a user profile page for my site, i’m trying to make it so you can edit your details e.g. email, location..ect.. but i’m having a little trouble with the edit section. It seems to edit the email like it should, but it doesn’t want to take it back to the profile page…. I’m sure this is something silly, but I can’t work it out. ?
here’s my script.

[code=php]
<?php session_start();?>
<?php
$username = $_SESSION[‘username’];

$conn = mysql_connect(“localhost”,”username”,”password”)
or die(“Could not connect”);
$rs = mysql_select_db(“unicyclepics”,$conn)
or die(“Could not select database”);

$sql1 = “SELECT * FROM users WHERE user_name = ‘$username'”;
$result = mysql_query($sql1) or die(mysql_error());
$r = mysql_fetch_assoc($result);

$email = $r[’email’];
?>
<?php
if( (!$_POST[’email2′]) )
{
$form =”<p>Email: $email</p>”;
$form.=”<form action=”$PHP_SELF””;
$form.=” method=”post”>Email:<br /> “;
$form.=”<input type=”text” name=”email2″”;
$form.=” value=”$email2″><br /><br />”;
$form.=”<input type=”submit” value=”Edit”>”;
$form.=”</form>”;
echo($form);
}
else
{ $conn = mysql_connect(“localhost”,”username”,”password”)
or die(“Could not connect”);
$rs = mysql_select_db(“unicyclepics”,$conn)
or die(“Could not select database”);
$sql = “UPDATE users SET email = ‘$email2’ WHERE user_name = ‘$username'”;
$result = mysql_query($sql,$conn)
or die(mysql_error());
header(‘Location: http://’.$_SERVER[‘HTTP_HOST’].’/profile.php’);
}
?>
[/code]

The error looks like this.

[code=html]Warning: Cannot modify header information – headers already sent by (output started at /home/fhlinux184/u/unicyclepics.co.uk/user/htdocs/edit.email.php:9) in /home/fhlinux184/u/unicyclepics.co.uk/user/htdocs/edit.email.php on line 84[/code]

And line 84 is the “header(‘Location:…” line.
If anyone can tell me where i’ve gone rong, i’ll be very happy. ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@bokehSep 15.2006 — Hmm! Only 38 lines there! Where is the rest? When sending headers it is imperative not to send any output before the headers. Start by removing the code marked in red. [CODE]<?php session_start();[color=red]?>
<?php[/color]
$username = $_SESSION['username'];

$conn = mysql_connect("localhost","username","password")
or die("Could not connect");
$rs = mysql_select_db("unicyclepics",$conn)
or die("Could not select database");

$sql1 = "SELECT * FROM users WHERE user_name = '$username'";
$result = mysql_query($sql1) or die(mysql_error());
$r = mysql_fetch_assoc($result);

$email = $r['email'];
[color=red]?>
<?php[/color]
if( (!$_POST['email2']) )
{
$form ="<p>Email: $email</p>";
$form.="<form action="$PHP_SELF"";
$form.=" method="post">Email:<br /> ";
$form.="<input type="text" name="email2"";
$form.=" value="$email2"><br /><br />";
$form.="<input type="submit" value="Edit">";
$form.="</form>";
echo($form);
}
else
{ $conn = mysql_connect("localhost","username","password")
or die("Could not connect");
$rs = mysql_select_db("unicyclepics",$conn)
or die("Could not select database");
$sql = "UPDATE users SET email = '$email2' WHERE user_name = '$username'";
$result = mysql_query($sql,$conn)
or die(mysql_error());
header('Location: http://'.$_SERVER['HTTP_HOST'].'/profile.php');
}
?> [/CODE]
Copy linkTweet thisAlerts:
@PJStewauthorSep 15.2006 — Hmm! Only 38 lines there! Where is the rest? When sending headers it is imperative not to send any output before the headers. Start by removing the code marked in red. [CODE]<?php session_start();[color=red]?>
<?php[/color]
$username = $_SESSION['username'];

$conn = mysql_connect("localhost","username","password")
or die("Could not connect");
$rs = mysql_select_db("unicyclepics",$conn)
or die("Could not select database");

$sql1 = "SELECT * FROM users WHERE user_name = '$username'";
$result = mysql_query($sql1) or die(mysql_error());
$r = mysql_fetch_assoc($result);

$email = $r['email'];
[color=red]?>
<?php[/color]
if( (!$_POST['email2']) )
{
$form ="<p>Email: $email</p>";
$form.="<form action="$PHP_SELF"";
$form.=" method="post">Email:<br /> ";
$form.="<input type="text" name="email2"";
$form.=" value="$email2"><br /><br />";
$form.="<input type="submit" value="Edit">";
$form.="</form>";
echo($form);
}
else
{ $conn = mysql_connect("localhost","username","password")
or die("Could not connect");
$rs = mysql_select_db("unicyclepics",$conn)
or die("Could not select database");
$sql = "UPDATE users SET email = '$email2' WHERE user_name = '$username'";
$result = mysql_query($sql,$conn)
or die(mysql_error());
header('Location: http://'.$_SERVER['HTTP_HOST'].'/profile.php');
}
?> [/CODE]
[/QUOTE]


Sorry, I should of explained that better, the <?php session_start();?> is in the head, and then there is HTML, then the rest is in the body. Is there a better way to get it to go back to the profile page? ?
Copy linkTweet thisAlerts:
@bokehSep 15.2006 — You need to get your order right. Logic first (output nothing), headers next, and lastly if you are not redirecting output HTML. No point outputing HTML and then redirecting someone before they even get a chance to read it.

Hopefully no one is going to recommend ob_start() here!
Copy linkTweet thisAlerts:
@PJStewauthorSep 15.2006 — You need to get your order right. Logic first (output nothing), headers next, and lastly if you are not redirecting output HTML. No point outputing HTML and then redirecting someone before they even get a chance to read it.

Hopefully no one is going to recommend ob_start() here![/QUOTE]


I'm probally just being stupid, but I don't quite understand what you meen. Could you explain a little more, or give me an example? :o
Copy linkTweet thisAlerts:
@bokehSep 15.2006 — [code=php]<?php

// logic here

if((1 + 1) === 2)
{
$output = 'true';
}
else
{
$output = 'false';
}

// headers here

header('Content-Type: text/html');

// followed by html
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<p>1 + 1 = 2 is <?php echo $output ?>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@PJStewauthorSep 16.2006 — Thanks again Boken! ?
×

Success!

Help @PJStew spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...