Cannot delete MYSQL entry from PHP

In my book(Build Your Own Database Driven Web Site Using PHP & MySQL, 4th Edition, by Kevin Yank) it has instructed me to put a delete option into my php scripts to allow for entries in my MySQL server to be deleted.

So far i have been able to bring up a list of entries, add entries but when i add the delete code it fails to work, all it results in is a refresh of the page, not even an error reported. Everything else works fine. The entries are still in the database after i have clicked on these buttons.

I have tried copying and then editing the scripts from the code archive but this has resulted in the same end result: nothing different occuring.

Below is the code for the two files which i believe contain the problem. There are two other files: error.html.php and form.html.php, these i do not believe are linked to the delete function so i have left them out of the post for the time being.

index.php


<!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>List of Items</title>
		<meta http-equiv="content-type"
				content="text/html; charset=utf-8"/>
	</head>
	<body>
		<p><a href="?additem">Add a new item to the database</a></p>
		<p>This list contains the current set of items that are located within the melee_weapons database:</p>
		<?php foreach ($items as $melee_weapons): ?>
			<form action="?deleteitem" method="post">
				<blockquote>
					<p>
						<?php echo htmlspecialchars($melee_weapons['text'], ENT_QUOTES,
								'UTF-8'); ?>
						<input type="hidden" name="id" value="<?php
								echo $items['id']; ?>"/>
						<input type="submit" value="Delete"/>
					</p>
				</blockquote>
			</form>
		<?php endforeach; ?>
	</body>
</html>

items.html.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['additem']))
{
	include 'form.html.php';
	exit();
}

$link = mysqli_connect('localhost', 'root', '//password would normally go here :)//');
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, 'swidb'))
{
	$error = 'Unable to locate the item database.';
	include 'error.html.php';
	exit();
}

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

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

if (isset($_GET['deletename']))
{
	$id = mysqli_real_escape_string($link, $_POST['id']);
	$sql = "DELETE FROM melee_weapons WHERE id='$id'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error deleting item: ' . mysqli_error($link);
		include 'error.html.php';
		exit();
	}

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

$result = mysqli_query($link, 'SELECT id, itemname FROM melee_weapons');
if (!$result)
{
	$error = 'Error fetching items: ' . mysqli_error($link);
	include 'error.html.php';
	exit();
}

while ($row = mysqli_fetch_array($result))
{
	$items[] = array('id' => $row['id'], 'text' => $row['itemname']);
}

include 'items.html.php';
?>

Conclusion

In advance, thank you for your help and i hope that i have stated enough information for you to work out a solution to my problem. :slight_smile:

I would suggest you (OP) to learn how to troubleshoot when you run into some problems:

  • Echo/pring the value of the variable which you are using in the script so that you know what value is coming.
  • Use print_r($array) if it is the array that you are using and not working properly.
  • View source of the page if your values are really printed in the form elements or not. You can also use firebug in firefox browser or IE Developer toolbar in IE.
  • Use some good PHP IDEs to be safe from syntactical errors.

Aha, this solved the problem. Thank you very much. :slight_smile:

This line


<input type="hidden" name="id" value="<?php echo $items['id']; ?>"/>

should be replaced with


<input type="hidden" name="id" value="<?php echo $melee_weapons['id']; ?>"/>

Because as far as I can see $melee_weapons holds the ID for the weapon, not $items, which is why you also use $melee_weapons in the line above the one I just pointed out :slight_smile:

I changed this value but the error still occurs.

From what I can see it should be


if (isset($_GET['deleteitem']))

instead of


if (isset($_GET['deletename']))

because the form action is ?deleteitem, not ?deletename

:slight_smile: