1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4. <head>
5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6. <title>Delete a Blog Entry</title>
7. </head>
8. <body>
9. <?php // Script 12.8 - delete_entry.php
10. /* This script deletes a blog entry. */
11.
12. // Connect and select:
13. $dbc = mysql_connect('h41mysql35.secureserver.net', 'yrosenthal', 'Password.1');
14. mysql_select_db('yrosenthal');
15.
16. if (isset($_GET['id']) && is_numeric($_GET['id']) ) { // Display the entry in a form:
17.
18. // Define the query:
19. $query = "SELECT title, entry FROM entries WHERE entry_id={$_GET['id']}";
20. if ($r = mysql_query($query)) { // Run the query.
21.
22. $row = mysql_fetch_array($r); // Retrieve the information.
23.
24. // Make the form:
25. print '<form action="script_12_08.php" method="post">
26. <p>Are you sure you want to delete this entry?</p>
27. <p><h3>' . $row['title'] . '</h3>' .
28. $row['entry'] . '<br />
29. <input type="hidden" name="id" value="' . $_GET['id'] . '" />
30. <input type="submit" name="submit" value="Delete this Entry!" /></p>
31. </form>';
32.
33. } else { // Couldn't get the information.
34. print '<p style="color: red;">Could not retrieve the blog entry because:<br />' . mysql_error() . '.</p><p>The query being run was: ' . $query . '</p>';
35. }
36.
37. } elseif (isset($_POST['id']) && is_numeric($_POST['id'])) { // Handle the form.
38.
39. // Define the query:
40. $query = "DELETE FROM entries WHERE entry_id={$_POST['id']} LIMIT 1";
41. $r = mysql_query($query); // Execute the query.
42.
43. // Report on the result:
44. if (mysql_affected_rows() == 1) {
45. print '<p>The blog entry has been deleted.</p>';
46. } else {
47. print '<p style="color: red;">Could not delete the blog entry because:<br />' . mysql_error() . '.</p><p>The query being run was: ' . $query . '</p>';
48. }
49.
50. print '<p>click <a href="script_12_07.php">here</a> to see all the blog posts.</p>';
51.
52.
53. } else { // No ID set.
54. print '<p style="color: red;">This page has been accessed in error.</p>';
55. } // End of main IF.
56.
57. mysql_close(); // Close the database connection.
58.
59. ?>
60. </body>
61. </html>
62.