Mock-CMS not deleting authors from database

Ok, I’m not sure if this is the correct forum to post this, but it’s PHP/MySQL related, so I figured I’d post it here.

Right now, I’m working through Kevin Yank’s “PHP & MySQL: Novice to Ninja - 5th Edition”, and I’m on Chapter 7, where you’re tasked with creating the back-end for a simple Joke CMS. I’m at the part where you’re supposed to add buttons for the user/admin. to be able to delete an author from the database (as well as any jokes they’ve added to the DB, and the category entries for those jokes), and while I can get the list of authors & the delete buttons to show properly, whenever I click the “Delete” button next to an author’s name, nothing happens in the database.

So I was wondering if anybody could tell me what’s wrong with my code?

For the controller (index.php):

<?php 

//If user clicked 'Delete' button, 'action' will be sent to "$_POST" & value should be 'DELETE'
if(isset($_POST['action']) and $_POST['action'] == 'Delete') {
	
	//Connect to database 
	include $_SERVER['DOCUMENT_ROOT'] . '/phpmysqlnovicetoninja/includes/db.inc.php';
	
	//Get jokes belonging to the author
	try {
		
		$sql = 'SELECT id FROM joke WHERE authorid = :id';
		$s = $pdo->prepare($sql);
		$s->bindValue(':id', $_POST['id']);
		$s->execute();
		
	} catch (PDOException $e) {
		
		$error = 'Error retrieving list of jokes to delete.';
		include $_SERVER['DOCUMENT_ROOT'] . '/phpmysqlnovicetoninja/includes/error.html.php';
		exit();
		
	}
	
	$result = $s->fetchAll();
	
	//Delete joke category entries
	try {
		
		$sql = 'DELETE FROM jokecategory WHERE jokeid = :id';
		$s = $pdo->prepare($sql);
		
		//For each joke
		foreach($result as $row) {
			
			$jokeId = $row['id'];
			$s->bindValue(':id', $jokeId);
			$s->execute();
			
		}
	
	} catch (PDOException $e) {
		
		$error = 'Error deleting category entries for joke.';
		include $_SERVER['DOCUMENT_ROOT'] . '/phpmysqlnovicetoninja/includes/error.html.php';
		exit();
		
	}
	
	//Delete jokes belonging to the author
	try {
		
		$sql = 'DELETE FROM joke WHERE authorid = :id';
		$s = $pdo->prepare($sql);
		$s->bindValue(':id', $_POST['id']);
		$s->execute();
		
	} catch (PDOException $e) {
		
		$error = 'Error deleting jokes for author';
		include $_SERVER['DOCUMENT_ROOT'] . '/phpmysqlnovicetoninja/includes/error.html.php';
		exit();
		
	}
	
	//Delete the author
	try {
		
		$sql = 'DELETE FROM author WHERE id = :id';
		$s = $pdo->prepare($sql);
		$s->bindValue(':id', $_POST['id']);
		$s->execute();
		
	} catch (PDOException $e) {
		
		$error = 'Error deleting author.';
		include $_SERVER['DOCUMENT_ROOT'] . '/phpmysqlnovicetoninja/includes/error.html.php';
		exit();
		
	}
	
	header('Location: .');
	exit();
	
}

//Display list of authors
include $_SERVER['DOCUMENT_ROOT'] . '/phpmysqlnovicetoninja/includes/db.inc.php';

try {
	
	$result = $pdo->query('SELECT id, name FROM author');
	
} catch (PDOException $e) {
	
	$error = 'Error fetching authors from the database!';
	include $_SERVER['DOCUMENT_ROOT'] . '/phpmysqlnovicetoninja/includes/error.html.php';
	exit();
	
}

foreach($result as $row) {
	
	$authors[] = array('id' => $row['id'], 'name' => $row['name']);
	
}

include 'authors.html.php';

?> 

And here’s the code for the “Manage Authors” template (authors.html.php):

<?php include $_SERVER['DOCUMENT_ROOT'] . '/phpmysqlnovicetoninja/includes/helpers.inc.php'; ?>
<!DOCTYPE HTML>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Manage Authors</title>
</head>
<body>

	<h1>Manage Authors</h1>
	<p/><a href="?add">Add Authors</a>
	<ul>
		<?php foreach($authors as $author): ?>
			<li>
				<form action="" method="post">
					<div>
						<?php htmlout($author['name']); ?>
						<input type="hidden" name="id" value="<?php echo $authors['id']; ?>" />
						<input type="submit" name="action" value="Edit" />
						<input type="submit" name="action" value="Delete" />
					</div>
				</form>
			</li>
		<?php endforeach; ?>
	</ul>
	<p/><a href="..">Return to JMS Home</a>

</body>
</html>

I have no idea what’s going wrong. The book says that at this point, clicking the “Delete” button next to an author’s name should delete the author from the “author” table in the DB, as well as any jokes they’ve submitted in the “joke” table & the joke’s categorical assignments in the “jokecategory” table, but whenever I click “Delete”, the page just reloads, and nothing is changed in the DB.

Can somebody tell me what’s going on?

This is a good reason to start learning about debugging right now.
First step here will be localize the problem. That means you need to know which part of your code doesn’t work as supposed. To find that, you can use breakpoints.

These lines should delete author from database:

$sql = 'DELETE FROM author WHERE id = :id';
$s = $pdo->prepare($sql);

You need to check if they work, in other words:

  1. these lines are executed when you click “Delete” button;
  2. they produce correct SQL-query.

Try to add this right after preparing the sql:

die($_POST['id']);

This will force your script to print contents of $_POST[‘id’] and terminate execution immediately on this line.
So, that means when you’ll click “Delete” button next time you should see blank page with only author ID printed. If you don’t see it, go one level up and insert die('foobar'); right after:

if(isset($_POST['action']) and $_POST['action'] == 'Delete') {

Then check if it prints something. Your main goal is to research how your script execution flows and find a place where it fails.

I hope you got the idea. Good luck.

Ok so, I found the problem…coding error on my part. :sweat: lol

What was happening was the script wasn’t attributing the author’s id to any authors in the databse when the “Delete” button was clicked.

But thanks for your help Megazoid. The die() function helped me track down the issue. :sunglasses:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.