BYO Database Site - Header Question

I’m working in Chapter 6 and am a bit stuck. I tried copying code directly from my book but I still am having errors.

When I try to add a new joke or delete a joke, I get this error,

Warning: Cannot modify header information - headers already sent by (output started at G:\\xampp\\htdocs\\sitepoint\\chapter6\\db.inc.php:1) in G:\\xampp\\htdocs\\sitepoint\\chapter6\\index.php on line 22

Line 22 is the header line here:

if (isset($_POST['joketext']))
	{
	include 'db.inc.php';
	$joketext = mysqli_real_escape_string($link, $_POST['joketext']);

	$sql = 'INSERT INTO joke SET
	joketext = "' . $joketext . '",
	jokedate = CURDATE() ';
	if (!mysqli_query($link, $sql))
		{
		$error = 'Error adding submitted joke: ' . mysqli_error($link);
		include 'error.html.php';
		exit();
		}
	header('Location: .');
	exit();
	}

I guess I’m a bit confused as to these headers in general. The actions are occuring as expected (the joke does actually make it to the DB / or a joke actually is deleted) , I just get the error first. I can’t figure out what is going on.

Full code for index.php:

<?php
if (isset($_GET['addjoke']))
	{
	include 'form.html.php';
	exit();
	}

if (isset($_POST['joketext']))
	{
	include 'db.inc.php';
	$joketext = mysqli_real_escape_string($link, $_POST['joketext']);

	$sql = 'INSERT INTO joke SET
	joketext = "' . $joketext . '",
	jokedate = CURDATE() ';
	if (!mysqli_query($link, $sql))
		{
		$error = 'Error adding submitted joke: ' . mysqli_error($link);
		include 'error.html.php';
		exit();
		}
	header('Location: .');
	exit();
	}
if (isset($_GET['deletejoke']))
	{
	include 'db.inc.php';
	$id = mysqli_real_escape_string($link, $_POST['id']);

	$sql = "DELETE FROM joke WHERE id='$id'";
	if (!mysqli_query($link, $sql))
		{
		$error = 'Error deleting joke: ' . mysqli_error($joke);
		include 'error.html.php';
		exit();
		}
	header('Location: .');
	exit();
	}

include 'db.inc.php';

$result = mysqli_query($link, 'SELECT joke.id, joketext, name, email FROM joke INNER JOIN author ON authorid = author.id');
if (!result)
	{
	$error = 'Error fetching jokes: ' . mysqli_error($link);
	include 'error.html.php';
	exit();
	}

while ($row = mysqli_fetch_array($result))
{
$jokes[] = array ('id' => $row['id'], 'text' => $row['joketext'], 'name' => $row['name'], 'email' => $row['email']);
}
include 'jokes.html.php';
?>

(I left out the Magic Quotes code because my php.ini has it turned off).

Earlier versions of this do seem to work. Also, I’m not seeing all my jokes on the jokes.html.php any more … only the ones from before.

I’m really at a loss!

(yeah, my first time coding :D)

Yeah I am using notepad++ and have the default set to UTF8. I just rewrote the entire thing and I’m still seeing the same issue, ha ha.

UPDATE: I just changed from UTF-8 to UTF-8 without BOM and it WORKS! so apparently taht was my issue.

I went back and looked and my older files were encodedin ANSII.

Thanks for the help!!

Hmm… this would be in the included file, not index.php? (error.html.php)?

It’s bizarre because I don’t see it happening on my Chapter 5 work, it seems to perform flawlessly there.

At this point I’m going to rewrite the code from scratch (good for me to practice anyway) and see if I get a different result. There must be something obvious I’m missing.

There are 2 parts to the error message
Line 22 is were the script is trying to send headers again.
Line 1 of db.inc.php is where headers were already sent.

Is db.inc.php supposed to output anything? Does it have an error?

No outputs… db.inc.php is included to estalish a database connection. It creates $link, connects to MySQL, sets character encoding, and finally connects to the database itself.

Thanks for parsing that error for me more - Iwasn’t aware it meant it was already sent on Line 1 of db.inc.php.

So this:

output started at G:\xampp\htdocs\sitepoint\chapter6\index.php:1

Means on index.php line #1?

The only thing on Line #1 is <?php

The error message means that output has already been sent before the header line is reached. Output can be html code, or an php echo command, or even a space or empty line outside of the php opening and closing tags. Check your file to see if there are any spaces or empty lines in front of the <?php opening tag.

Oh, also –> in general, I’m not really clear on what Header is actually doing… some sort of redirect but I didn’t get a good feel for it from the book.

**I think I may know why not all the jokes are showing up. I did play with the code a bit to include the fields from Chapter 5 (author and name) using the INNER JOIN. Since they aren’t all included on my “add a joke” form –> could that be what the problem is? (the add a joke form just adds the joketext).

maybe the entirething is my fault for mixing up chapters but I’m trying to play around a bit so I can hav a better understanding of ho the parts fit together.

TIA!

(I just read the oening too - sorry if this should have been posted in the PHP forum instead!)

Actually the most common header() error with line 1 isn’t obvious at all. It’s almost always a BOM (Byte Order Mark).

The BOM is “invisible” and only serves to indicate what order bytes should be read. Big Endian or Little Endian. i.e.

if the bytes are
00110110 01011000 …
are they read left to right or right to left?

If that’s too confusing, don’t worry about it now.

I’m guessing you’re saving your files as UTF-8. Some text editors call the BOM “Signature”. In any case make double sure there’s nothing before the “<”, no newlines, no spaces, nothing. Then try saving the files without it.

If line 22 is the one i signed in red, then the included wasn’t executed. If it was, line 22 would not have been executed (the exit() after the include ends the execution of the script).

if (isset($_POST['joketext']))
    {
    include 'db.inc.php';
    $joketext = mysqli_real_escape_string($link, $_POST['joketext']);

    $sql = 'INSERT INTO joke SET
    joketext = "' . $joketext . '",
    jokedate = CURDATE() ';
    if (!mysqli_query($link, $sql))
        {
        $error = 'Error adding submitted joke: ' . mysqli_error($link);
        include 'error.html.php';
        exit();
        }
[B][COLOR="Red"]    header('Location: .');[/COLOR][/B]
    exit();
    }