Help needed with the "add_comment" function in the blog tut.

Post here is you are having problems with any of the tutorials.
Post Reply
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Help needed with the "add_comment" function in the blog tut.

Post by nyo »

Hi,

I am working on the add_comment function and I have a slight issue I don't know how to solve. Here is the function I want to use:
function add_comment($name, $author, $email, $body) {
	if(valid_post($name) == false){
		return false;
	}
	$name = mysql_real_escape_string(htmlentities($name));
	$author = mysql_real_escape_string(htmlentities($author));
	$email	= mysql_real_escape_string(htmlentities($email));
	$body = mysql_real_escape_string(nl2br(htmlentities($body)));
	mysql_query("INSERT INTO `comments` (`com_post_id`, `com_author`, `com_author_email`, `com_body`, `com_date`) VALUES ('$name', '$author', '$email', '$body', NOW())");
	return true;
}
I am getting the post by its name and not the id. I need to match `com_post_id` with the `posts`.`id` but I have $name (`post_name`) available. I checked WordPress to see how the commenting is handled but it looks too complicated. The reason I am getting posts by $name is that I want nice looking urls such as http://www.mysite.com/this-is-a-post, and here post_name = this-is-a-post.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Help needed with the "add_comment" function in the blog

Post by jacek »

It might be easier to still work with ids, you could have the url as http://www.mysite.com/8/this-is-a-post

Where 8 is the post id and the name is just there for decoration.
Image
Post Reply