Deleting from a database using PHP

I’ve been working through Kevin Yank’s “Build you own database driven site using php and mysql” and have had great success until now. I’m at the end of Chapter 4, and trying to delete an item from a database (his example is a joke).

The problem is when I go to my localhost:8080/deletejoke, I get the error message:
Apache HTTP Server has encountered a problem and needs to close. We are sorry for the inconvenience.

The thing is, all my other links in my local host work, for example, when I type in localhost:8080/deletejoke/?addjoke, I still get my add joke form and can successfully add them. Plus, my localhost:8080/addjoke page works too.

Here’s the script for my deletejoke/index.php

<?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', 'kjhgfdsa');
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))
{
	$joke[] = array('id' => $row['id'], 'text' => $row['joketext']);
}

include 'jokes.html.php';
?>

I don’t think the problem is with the jokes.html.php page, but here that is anyway:

<!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>List of Jokes</title>
</head>

<body>
<p><a href="?addjoke">Add your own joke</a></p>
<p>Here are all the jokes in the database:</p>
<?php foreach ($jokes as $joke): ?>
<form action="?deletejoke" method="post">
<blockquote><p>
	<?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8'); ?>
    <input type="hidden" name="id" value="<?php echo $joke['id']; ?>"/>
    <input type="submit" value="Delete"/>
</p></blockquote>
</form>
<?php endforeach; ?>
</body>
</html>

So, I’ve retyped and looked over my scripts like a 1000 times, and can’t find anything wrong with them. Any help would be invaluable and I appreciate anyone who can offer some insight.

Thanks!

Run this script and post the versions output (it basically identifies the versions of Apache, MySQL and PHP that your using:

<?php
$apache_version            = $_SERVER['SERVER_SOFTWARE'];
$php_version            = phpversion();
$link                    = mysqli_connect("localhost", "db_user_name", "db_password");
$mysql_server_version    = mysqli_get_server_info($link);

echo "
    <p>Apache (Version: $apache_version) is running the following versions of PHP and MySQL:</br>
    PHP: $php_version</br>
    MySQL: $mysql_server_version</p>
    ";
?>

Substitute db_user_name and db_password for the relevant values.

Apache (Version: Apache/2.2.14 (Win32) PHP/5.2.12) is running the following versions of PHP and MySQL:

PHP: 5.2.12
MySQL: 5.1.42-community

Did you install them separately or as a WAMP server? What errors show up in Apache’s error log (the file is on the server called apache_error.log) and PHP’s error log (the file is on the server called php_error.log)?

Installed seperately.

Apache error log:

[Thu Jan 21 18:36:44 2010] [notice] Server built: Sep 28 2009 22:41:08
[Thu Jan 21 18:36:44 2010] [notice] Parent: Created child process 1196
[Thu Jan 21 18:36:45 2010] [notice] Child 1196: Child process is running
[Thu Jan 21 18:36:46 2010] [notice] Child 1196: Acquired the start mutex.
[Thu Jan 21 18:36:46 2010] [notice] Child 1196: Starting 64 worker threads.
[Thu Jan 21 18:36:46 2010] [notice] Child 1196: Starting thread to listen on port 8080.
[Thu Jan 21 18:37:37 2010] [notice] Parent: child process exited with status 3221225477 -- Restarting.
[Thu Jan 21 18:37:38 2010] [notice] Apache/2.2.14 (Win32) PHP/5.2.12 configured -- resuming normal operations
[Thu Jan 21 18:37:38 2010] [notice] Server built: Sep 28 2009 22:41:08
[Thu Jan 21 18:37:38 2010] [notice] Parent: Created child process 2568
[Thu Jan 21 18:37:41 2010] [notice] Child 2568: Child process is running
[Thu Jan 21 18:37:41 2010] [notice] Child 2568: Acquired the start mutex.
[Thu Jan 21 18:37:41 2010] [notice] Child 2568: Starting 64 worker threads.
[Thu Jan 21 18:37:41 2010] [notice] Child 2568: Starting thread to listen on port 8080.

Can’t find the PHP error log.

Think I may have fixed it though.

Copied libmysql.dll file from the PHP installation folder and saved it to windows\system32 folder.

Restarted Apache server.

It’s showing correctly now, but I’m worried this is only a cover fix and not a thorough fix.

Any advice?

It looks like it’s a bug with PHP that was the initial cause.