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)