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?