Hey guy , while reading the book i got stuck on one place , i dont know what is the problem in my code , when i hit the delete button it deletes the jokes and the author from the db but it doens't delete the relations from jokecategory , this is my code:
author.html.phpCode:<?php include $_SERVER['DOCUMENT_ROOT'] . '/training/preparations.php'; include $_SERVER['DOCUMENT_ROOT'].'training/funkcii/includes/functions.php'; try { $result = $pdo->query('SELECT id, name FROM author'); } catch (PDOException $e) { $error = 'Error fetching authors from the database!'; include 'error.html.php'; exit(); } foreach ($result as $row) { $authors[] = array('id' => $row['id'], 'name' => $row['name']); } include 'author.html.php'; if (isset($_POST['action']) and $_POST['action'] == 'Delete') { 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 fetching the jokes to delete'.$e->getMessage(); echo $error; exit(); } $result = $s->fetchAll(); try { $sql = 'DELETE FROM jokecategory WHERE jokeid = :id'; $s = $pdo->prepare($sql); foreach ($result as $row) { $jokeId = $row['id']; $s->bindValue(':id', $jokeId); $s->execute(); } } catch (PDOException $e) { $error = 'Error deleting category entryes for joke'.$e->getMessage(); echo $error; exit(); } try { $sql = 'DELETE FROM joke WHERE authorid = :id'; $s = $pdo->prepare($sql); $s->bindValue(':id',$_POST['id']); $s->execute(); } catch (PDOException $e) { echo 'Error deleting jokes from this author'.$e->getMessage(); exit(); } try { $sql = 'DELETE FROM author WHERE id = :id'; $s = $pdo->prepare($sql); $s->bindValue(':id',$_POST['id']); $s->execute(); } catch (PDOException $e) { echo 'Error deleting the author'.$e->getMessage(); exit(); } header('Location: .'); exit(); } ?>
Code:<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Manage Authors</title> <link rel="stylesheet" type="text/css" href="../../css/style.css" /> </head> <body> <h1>Manage Authors</h1> <p><a href="">Add new author</a></p> <ul> <?php foreach ($authors as $author): ?> <li> <form action="" method="post"> <div> <?php htmlout($author['name']); ?> <input type="hidden" name="id" value="<?php echo $author['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></p> </body> </html>


Reply With Quote
Bookmarks