Header (Location) Function Not Working

Hi,

First post whilst I start to learn, so sorry to be a plum…

However, I am following the book ‘Build Your Own Database Driven Website Using PHP & MYSQL’ by Kevin Yank.

I have followed the book up until page 147 but I have a problem. This page lists the complete index.php code for displaying jokes from a database, adding jokes and deleting jokes:

<?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(‘localhost’, ‘root’, ‘password’);
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, ‘ijdb’))
{
$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 . ‘",
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’]))
{
$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($link);
include ‘error.html.php’;
exit();
}
header(‘Location: .’);
exit();

}
$result = mysqli_query($link, ‘SELECT id, joketext FROM joke’);
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’]);
}
include ‘jokes.html.php’;
?>

I can get the page to display the jokes list and even add a joke. It then returns to the jokes list (the purpose of including the header (location) function).

BUT, when I try to delete a joke nothing happens, the page looks the same and all the jokes are still there. I check the database using MySQL Admin and nothing has changed.

Any ideas why? I initially had a problem getting the header(location) bit to work when adding jokes but deleting the following exit(); solved it. I have copied and pasted the complete code example into my own index file and it now works again with the exit(); now replaced.

Help?

Thanks,
Stuart

please wrap PHP code in the appropriate tags when posting…


<?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('localhost', 'root', 'password');
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, 'ijdb'))
{
	$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 . '",
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']))
{
	$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($link);
		include 'error.html.php';
		exit();
	}
	header('Location: .');
	exit();
}
$result = mysqli_query($link, 'SELECT id, joketext FROM joke');
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']);
}
include 'jokes.html.php';
?>

show us the form you use to access this page.

Got it sorted, thanks anyway.
I hit enter when trying to format a line without realising it was part of the line.

Stu