Can anyone help me, i’m sure its a fairly obvious question but im new to php and building my first blog site using the really good tutorials from http://codegrrl.com/.
The problem I am having comes when I try to update an entry. I think the problem is with the redirecting of the page when the updated entry is submitted using the
header("Location: viewEntry.php?id=".$id);
I get this error message :
Warning: Cannot modify header information - headers already sent by (output started at /path/to/my/domain.com/html/Blog/update.php:9) in /path/to/my/domain.com/html/Blog/up.php on line 40
This is the code that I am using
<head>
<title>update entry</title>
</head>
<body>
<?php
require_once('connect to domain.php');
mysql_select_db ($database_table);
$current_month = date("F");
$current_date = date("d");
$current_year = date("Y");
$current_time = date("H:i");
//update
if(isset($_POST['update'])) {
$id = htmlspecialchars(strip_tags($_POST['id']));
$month = $_POST[$current_month];
$date = $_POST[$current_date];
$year = $_POST[$current_year];
$time = $_POST[$current_time];
$entry = $_POST['entry'];
$title = htmlspecialchars(strip_tags($_POST['title']));
if (isset($_POST['password'])) $password = htmlspecialchars(strip_tags($_POST['password']));
else $password = "";
$entry = nl2br($entry);
if (!get_magic_quotes_gpc()) {
$title = addslashes($title);
$entry = addslashes($entry);
}
$timestamp = strtotime ("$date . $month . $year . $time");;
$result = mysql_query("UPDATE php_blog SET timestamp='$timestamp', title='$title', entry='$entry', password='$password' WHERE id='$id' LIMIT 1") or print ("Can't update entry.<br />" . mysql_error());
header("Location: viewEntry.php?id=".$id);
}
if (!isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id'])) {
die("Invalid entry ID.");
}
else {
$id = (int)$_GET['id'];
}
$result = mysql_query ("SELECT * FROM php_blog WHERE id='$id'") or print ("Can't select entry.<br />" . $sql . "<br />" . mysql_error());
while ($row = mysql_fetch_array($result)) {
$old_timestamp = $row['timestamp'];
$old_title = stripslashes($row['title']);
$old_entry = stripslashes($row['entry']);
$old_password = $row['password'];
$old_title = str_replace('"','\'',$old_title);
$old_entry = str_replace('<br />', '', $old_entry);
$old_month = date("F",$old_timestamp);
$old_date = date("d",$old_timestamp);
$old_year = date("Y",$old_timestamp);
$old_time = date("H:i",$old_timestamp);
}
?><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><input type="hidden" name="id" value="<?php echo $id; ?>"/>
<p><strong><label for="title">Title:</label></strong> <input type="text" name="title" id="title" value="<?php echo $old_title; ?>" size="40" /> </p>
<p><strong><label for="password">Password protect?</label></strong> <input type="checkbox" name="password" id="password" value="1"<?php if($old_password == 1) echo " checked=\"checked\""; ?>/></p>
<p><textarea cols="80" rows="20" name="entry" id="entry"><?php echo $old_entry; ?></textarea></p>
<p><input type="submit" name="update" id="update" value="Update"></p>
</form>
<?php
mysql_close();
?>
</body>
</html>
So basically the page has a form that fills with the original data from the entry and you can change it then click update.
After searching the problem in google most sources seem to be saying that it is a whitespace problem but I have no idea what that means in reference to my file.
I would be really greatful if anyone can help
Thanks