Cedant web hosting - WRITE YOUR OWN BLOG ENGINE // direct to

WRITE YOUR OWN BLOG ENGINE // direct to post page to eliminate repeat posts header(”Location: post.php?post_id=$post_id&message=$message”); } } This code inserts a new comment into the database and e-mails you when a new comment is added. Stepping through the code: if (isset($_POST[”postcomment”]) != “”) { If the Post comment button has been pressed, the comment can be added to the database. $posttitle = addslashes(trim(strip_tags($_POST[”posttitle”]))); $name = addslashes(trim(strip_tags($_POST[”name”]))); $email = addslashes(trim(strip_tags($_POST[”email”]))); $website = addslashes(trim(strip_tags($_POST[”website”]))); $comment = addslashes(trim(strip_tags($_POST[”comment”]))); The post title (sent as a hidden field), commenter s name, e-mail address, website, and comment are all gathered from the query string and are tidied up by nesting three functions. Firstly strip_tags eliminates any HTML and PHP from the comment input, then trim removes white space (such as spaces and line breaks) from either end of the input, and finally addslashes ensures the text is formatted suitably for database entry. $sql = “INSERT INTO comments (post_id,name,email,website,comment) VALUES (’$post_id’, ‘$name’, ‘$email’, ‘$website’, ‘$comment’)”; $result2 = mysql_query($sql); if (!$result2) { $message = “Failed to insert comment.”; } else { $message = “Comment added.”; The database insert is performed. Note that the post_id is inserted with the comment so in future the blogging engine will know which comments go with which posts. $comment_id = mysql_insert_id(); As when inserting a new post, the comment_id for a new comment is automatically generated by MySQL. The latest id can be retrieved using the mysql_insert_id function. Now comes part of the script which e-mails yourself the comment so you know as soon as someone comments on your blog. PHP uses the mail function to send e-mail. An e-mail is made up of 4 four parts: the to address, the subject, the body, and the headers (which contain additional information such as the sender s details and cc d addresses). The mail function works like this: mail(to address, subject, body, headers) 315

Leave a Reply