Build Your Own DB Driven book chapter 7 confirm delete

Did anyone ever solve this?

[QUOTE=Caffeinerush;4469391]Hi, I’m up to the point that challenges to confirm deleting a user from the database instead of 1 click deleting.

So far the ID of the user to delete is being picked up by a hidden form field in the template, but when I click the “yes” button, the form value doesn’t seem to submit, so the delete (if statement) doesn’t run.

Here’s the delete code from the controller (index.php):


// confirm deletion
if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
	include $_SERVER['DOCUMENT_ROOT'] . '/phpmysql_book/includes/db.inc.php';
	$id = mysqli_real_escape_string($link, $_POST['id']);
	include 'confirm.html.php';
	exit();
}
// delete author, and their jokes
if (isset($_POST['authorid']))
{
	include $_SERVER['DOCUMENT_ROOT'] . '/phpmysql_book/includes/db.inc.php';
	//$id = mysqli_real_escape_string($link, $_POST['id']);
	// Get jokes belonging to author
	$sql = "SELECT id FROM joke WHERE authorid='$id'";
	$result = mysqli_query($link, $sql);
	if (!$result)
	{
		$error = 'Error getting list of jokes to delete.';
		include 'error.html.php';
		exit();
	}
	// For each joke
	while ($row = mysqli_fetch_array($result))
	{
		$jokeId = $row[0];
		// Delete joke category entries
		$sql = "DELETE FROM jokecategory WHERE jokeid='$jokeid'";
		if (!mysqli_query($link, $sql))
		{
			$error = 'Error deleting category entries for joke.';
			include 'error.html.php';
			exit();
		}
	}
	// Delete jokes belonging to author
	$sql = "DELETE FROM joke WHERE authorid='$id'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error deleting jokes for author.';
		include 'error.html.php';
		exit();
	}
	// Delete the author
	$sql = "DELETE FROM author WHERE id='$id'";
	if (!mysqli_query($link, $sql))
	{
		$error = 'Error deleting author.';
		include 'error.html.php';
		exit();
	}
	header('Location: .');
	exit();
}

This is the confirm template(confirm.html.php):


...
<h1>Delete Author Confirmation</h1>
<p>Are You sure you want to delete the author? This action is permanent.</p>

<form action="?" method="post">
	<p>
		<input type="hidden" name="authorid" value="<?php echo $id; ?>"/>
		<input type="submit" value="Yes"/>
	</p>
</form>

</body>
</html>

Thanks.[/QUOTE]

It doesn’t look like it was in the thread.

That was Caffeinerush’s first and only post. Nobody replied so hopefully (s)he figured out the problem. I can’t see any obvious problem with the code that was posted, so I’m guessing it was something else (a typo somewhere?).

In any case, lots of other people have used that book and I’ve never seen anyone else with that or a similar problem - whatever it was.

Below is just the first part of the “Delete the author and their jokes section”.

if (isset ($_GET[‘authorid’]))
{
include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’;
$id = mysqli_real_escape_string($link, $_POST[‘id’]);

  1. Shouldn’t it be $_GET instead of $_POST?
  2. The //$id = mysqli_real_escape_string($link, $_POST[‘id’]);
    needs to be uncommented.
  3. On the form… I also put an action name… but it should just work with the “?” in the action.

It seems to be working for myself.