Problem with new Sitepoint book. Database driven website

Ok, so I picked up the book for Building your own database driven website with php and myql. Followed the information right up to page 141 without a problem. Aside from the fact that I feel things are too far spread out over too many files, (But I am still relatively inexperienced so it may just be me) it all seems to work until he tells me to place this line in the code: header('Location: . '); I’ll include the code in a minute, but every time I run the file, I get this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/85/6905585/html/tests/database connect/output.html.php:11) in /home/content/85/6905585/html/tests/database connect/index.php on line 107

I know from a really really basic beginner’s class that this is the result of trying to manipulate the headers after they have already been submitted. So how do I get around this problem successfully? Cause the other problem I had was that after I modified the code to be completely dependent on just the index.php file, it does exactly what he warned it would do. Resubmits the joke again and again. So enough babbling. Here’s the code:


	if (get_magic_quotes_gpc())
		function stripslashes_deep($value)
			$value = is_array($value) ?
			array_map('stripslashes_deep', $value) :
			return $value;
		$_POST = array_map('stripslashes_deep', $_POST);
		$_GET = array_map('stripslashes_deep', $_GET);
		$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
		$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
		if (isset($_GET['addjoke']))
		include 'form.php';

	$link = mysqli_connect('********************', '************', '****');
	if (!$link)
		$output = 'Unable to connect to the database server' .mysqli_error($link). ' ';
		include 'output.html.php';
	if (!mysqli_set_charset($link, 'UTF8'))
		$output = 'unable to set database connection encoding ' .mysqli_error(). ' ';
		include 'output.html.php';
	if (mysqli_select_db($link, 'dante2'))
		$output = 'Database connection established.';
	include 'output.html.php';
		$output = 'Unable to locate the dante2 database. ' .mysqli_error($link). ' ';
		include 'output.html.php';
		if (isset($_POST['joketext']))
		$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 'errorr.html.php';
		header ('Location: . ');
	$result = mysqli_query($link, 'SELECT joketext FROM joke');
	if (!$result)
		$error = 'Error fetching jokes: ' .mysqli_error($link);
		include 'error.html.php';
	while ($row = mysqli_fetch_array($result))
		$jokes[] = $row['joketext'];
	include 'jokes.html.php';

Any pointers would be greatly appreciated. =)

That error is generated typically when output has been sent before your header() call.

Output includes output from echo statements and blank lines in your code.

Remove any blank lines above the header() call in your posted code.

I see your code has an include file.

[COLOR=#000000][COLOR=#007700]include [/COLOR][COLOR=#DD0000]'output.html.php'[/COLOR][COLOR=#007700];[/COLOR][/COLOR]

Make sure that if the include() is executed, it doesn’t contain any echo statements or blank lines either.

Yep, I got it. The instructions said to have a bunch of xhtml in the output file along with doctype and all. I stripped all the xhtml from the file and that corrected the header issue. Now I just need to get the option to display for adding a new joke. It’s supposed to work after you add a joke. But all I’m getting is a list of the jokes without an option to add more.

If that’s what the instructions say then it looks like the author of the book is not sure what (s)he is doing because I don’t see how they expected it to work. But I don’t have a copy of the book so I don’t know why the instructions are as you say.

Hopefully someone with a copy of the book can help you with your option display problem or else post your code to add a new joke so others might be able to help as well.

Well, here’s the code for adding a new joke.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="" xml:lang="en" lang="en">
<title>Add Joke</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
<style type="text/css">
textarea {
display: block;
width: 100%;
<form action="?" method="post">
<label for="joketext">Type your joke here:</label>
<textarea id="joketext" name="joketext" rows="3" cols="40">
<div><input type="submit" value="Add"/></div>

Oddly enough, when I stripped all the html out of the output.html.php file, it would display all of the jokes just fine. And the link to give you the option to post a new joke. The problem then is that after you hit submit on the form, you get that same header error message.

Wasn’t the header issue resolved in post 3?

It was resolved until I got the form to show up. Then I got the same issue all over again. Once you submit the new joke from the form, you get the header issue all over. It makes me want to pull my eyes out right about now.

oh ok :frowning:

I don’t have a copy of the book.

Is the author of the book a SP member? If so, maybe try sending them or hawk a pm to get clarification on the instructions in the book.

Well I know that he is an author for sitepoint. Not sure what his screen name would be, though. Have to do some digging next. I’m trying to modify the index.php file again. See if I can come up with some kind of solution while I look for answers.

If you’re still having trouble then you can email for support. I’ll also see if Kev has a sec to drop in and check this thread.

Thanks, I’ll send the email now. Still working on files trying to come up with a solution. I really need to understand what I’m doing wrong. And given the fact that I seem to be the only one with this problem, I’m betting it is something I am doing.

Wonder of wonders and miracles of miracle! I’ve been hacking at this script for almost 3 days now non stop. I’m still convinced I did something wrong somewhere, but I managed to get the whole thing to work as it should. When I was modifying the control script to be independent from the error.html.php, form.html.php and other scripts, I had to place all of their print statements into the index.php file so the information from the script could be seen on the monitor.(duh) \

After I did this I was still getting the same errors and it puzzled me, cause I wasn’t relying on external files with html headings and such. Then it hit me, At the top of the control structure I left the IF statement for checking to see if someone wanted to add a joke. it turns out that it was the html print out from this If statement in conjunction with the echo from the database select call that was messing everything up. Once I moved the If statement below the header call and removed the connection confirmation for the database (it’ll be self evident if it can’t connect) Everything worked just fine.

I tried moving these statements around in the original control script and I met with equally pleasing results. I’m going to move on with the rest of the book, but I would still like to know what it is that I did wrong in the first place. Obviously these scripts were tested before they were printed, so I’m sure I goofed somewhere. Thank you for all of your help and suggestions. =) Very much appreciated.

Haha, that’s great news. :slight_smile:

Tell me about it. Couldn’t sleep for hours afterwards, I was so excited. Simple minds, simple pleasures. :wink: I would still be interested in speaking to Ken if he is not too busy. I’m sure I could learn a thing or two about what was going on. =)

And more great news! Not that I think anyone is following this anymore, but I’m going to mention it anyways. As I suspected, the book was correctly tested and printed. The fault was my own as I messed up one of the database connection statements. I had it printing out a message every time it successfully connected. The subsequent html that printed out as a result was sending the headers and causing the error that I kept encountering. Mr. Yank is a very good programmer, indeed. All of my other lessons from other books and people had all kinds of ifelse statements, but we never relied on a single controller script as with this book. Very appreciative for what I have gotten from this book so far. I still have much to learn.