I have found various other threads about this problem but none of the solutions seem to be working for me. I apologise for any obvious mistakes, this is my first go at PHP.
I am working through Kevin Yank’s How to build a database driven website using php and mysql 4th edition. I am currently at a section which goes through the process of adding information to a MYSQL database via a form on a website.
After the form is submitted the browser is supposed to load the controller(index.php) which adds information to the database, then re-loads the controller again (without the form submission) using
header(‘Location: .’);
exit();
causing it to display the list of data, including the newly added row.
The issue I am having is that everytime the form is submitted the data is added to the database then I receive the following error message:
Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\addjoke\index.php:8) in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\addjoke\index.php on line 65
I have read that nothing can be outputted before the header statement, and as far as I am aware nothing is. Line 8 is where my php script starts, there is no space before it, there is also no space after it ends.
I have tried copying and pasting literally the whole code out of the PDF book to see if I was entering something wrong and still received the same error. I also tried substituting the header statement for $_SERVER[‘PHP_SELF’] as the book suggests this is an alternative. Although I received no error with this, it redirected me to a blank page “localhost/addjoke/?” instead of reloading the index.
Here is my index.php:
[I]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />
<title>Header</title>
</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);
}
$link = mysqli_connect(‘localhost’, ‘root’, ‘password’);
if(!$link)
{
$output = ‘Unable to connect to database server.’;
include ‘output.php’;
exit();
}
if(!mysqli_set_charset($link, ‘utf8’))
{
$output = ‘Unable to set database encoding.’;
include ‘output.php’;
exit();
}
if(!mysqli_select_db($link, ‘jokes’))
{
$output = ‘Unable to locate the joke database.’;
include ‘output.php’;
exit();
}
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 ‘error.html.php’;
exit();
}
header(‘Location:.’);
exit();
}
if (isset($_GET[‘addjoke’]))
{
include ‘form.php’;
exit();
}
$result = mysqli_query($link, ‘SELECT joketext FROM joke’);
if (!$result)
{
$error = ‘Error fetching jokes:’ . mysqli_error($link);
include ‘error.php’;
exit();
}
while ($row = mysqli_fetch_array($result))
{
$jokes = $row[‘joketext’];
}
include ‘jokes.php’;
?></body>
</html>[/I]
Once again, sorry if I am making a stupid mistake. Any help would be greatly appreciated, thanks.