Build your own database driven website using php & mysql (3rd edition)

i keep gettng this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/33/6924633/html/practice/index.php:10) in /home/content/33/6924633/html/practice/index.php on line 68

here is my source code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>PHP Error</title>
        <meta http-equiv="content-type"
                content="text/html; charset=utf-8"/>
    </head>
    <body>
<?php
if (get_magic_quotes_gpc())
{
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($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.html.php';
    exit();
}

$link = mysqli_connect('[redacted]', '[censored]', '[unpublishable]');
if (!$link)
{
    $error = 'Unable to connect to the database server.';
    include 'error.html.php';
    exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
    $output = 'Unable to set database connection encoding.';
    include 'output.html.php';
    exit();
}

if (!mysqli_select_db($link, 'ddd19'))
{
    $error = 'Unable to locate the joke database.';
    include 'error.html.php';
    exit();
}

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

$result = mysqli_query($link, 'SELECT joketext FROM joke');
if (!$result)
{
    $error = 'Error fetching jokes: ' . mysqli_error($link);
    include 'error.html.php';
    exit();
}

while ($row = mysqli_fetch_array($result))
{
    $jokes[] = $row['joketext'];
}

include 'jokes.html.php';?>
</body>
</html>

can anyone tell me how to fix this error?

thanks in advance

It’s not allowed to use the header function if the PHP code has already produced some output.

Any header statement must occur before output from the PHP. See the header documentation page for more details about this.

If need be, take a look at the error.html.php file that’s included in the free download of all code used in the book.

this book is 4th edition not 3rd. i am doing the examples from chapter 4 page 140 - 141.

this is exactly how it is written in the book.

Then that book has an error in it (book authors are human too) which has since been fixed.

These two lines are spurious:


header(' Location: .');
exit();

Whether that means deleting them, or moving the if statement they are in further up, depends some some different factors.

Even the 4th edition has some problems, listed here in the errata

ok I just deleted it. Thanks for the responses! :slight_smile: