Web hosting unlimited bandwidth - WRITE YOUR OWN BLOG ENGINE // If delete

WRITE YOUR OWN BLOG ENGINE // If delete has a valid post_id $delete = (isset($_REQUEST[”delete”]))?$_REQUEST[”delete”]:”"; if (preg_match(”/^[0-9]+$/”, $delete)) { $sql = “DELETE FROM posts WHERE post_id = $delete LIMIT 1″; $result = mysql_query($sql); if (!$result) { $message = “Failed to delete post $delete. MySQL said . ” . mysql_error(); } else { $message = “Post $delete deleted.”; } } The script is almost identical to that used for updating a post. The only slight difference is the SQL query itself: DELETE FROM table WHERE condition is met LIMIT max num of records to act upon The WHERE clause works in an identical way to that used with UPDATE and SELECT. I have added a LIMIT clause as a safety measure to ensure that the DELETE command deletes only a single row. The LIMIT clause can also be used with UPDATE and SELECT statements. As I said earlier, MySQL has no inbuilt undo functionality so the DELETE command is dangerously simple. For example, this command will delete all the posts in your table! DELETE FROM table Note that when your post is deleted, the remaining post_ids do not renumber. So if you have three posts and delete post number 2, the remaining posts will still be numbered 1 and 3, and any subsequent new post will be numbered 4. This is a feature of MySQL that ensures database entries remain uniquely identified with the same id. Figure 7-10 shows the result. Figure 7-10. Your CMS index page showing the first post and delete link Finishing touches To tie together the CMS functionality, you need a way of navigating between the Index page and the Add a Post page, and back. A neat way to achieve this is with another include. The following code simply links between the two CMS pages you have created thus far:

295

WordPress database error: [Table 'armadillowebhosting_com_-_apache.wp_comments' doesn't exist]
SELECT * FROM wp_comments WHERE comment_post_ID = '366' AND comment_approved = '1' ORDER BY comment_date

Leave a Reply