Jump to content

Recommended Posts

I have a web site. It is all about tutorials on computer tech stuffs. Now I need to add comment feature on every article I write. I'm really sick of finding a good tutorial on php comment script. I found some scripts but they are putting their ad on my web site (I hate it). So can anyone guide to add php comment to my web site ? please help me :oops:

 

Thank you very much

 

Well that would depend on how you want to go about it. Do you have a database? Or do you plan to use text files, or will you be editing html files directly? Do you know how to sanatize user inputs? Do you plan on collecting some of the information that users submit for use latter? These are just a few of the questions you need to ask yourself. Also before you can just jump in you need to have a plan. Post back when you know what you want to do and you think you are ready to begin.

Well that would depend on how you want to go about it. Do you have a database? Or do you plan to use text files, or will you be editing html files directly? Do you know how to sanatize user inputs? Do you plan on collecting some of the information that users submit for use latter? These are just a few of the questions you need to ask yourself. Also before you can just jump in you need to have a plan. Post back when you know what you want to do and you think you are ready to begin.

 

Thank you so much for you reply WolfRage . Yes I have a MySQL data base on my hosting server and I can create new data base if you want (And i do not want to write it in to a text file) My website is static but i can convert it to php as well What I want is simple php comment script. Yes I would love to collect some user data they submit :)  Actually this is what i want 

 

http://www.labnol.org/internet/google-image-search-problem/12671/ 

 

Please help me

Thank you

Ok this will be a bit of a challenge for me, but that is because I try to avoid databases (SQL is not my fortay); but it will be a learning opportunity for me.

Just a little more planning before we begin. The database that you have does it already contain data similiar to the type of information we will be collecting? If so we could just give the comments a unique table in the database.

Have you already tested your server for PHP capability? Also how much understanding do you have of PHP?

Initially we are going to need a place to store the data. = In the database, either a seperate one or a unique table.

We will need to build a comment form to collect the information from the user.

We will have to build a php script that will be responsible for catching the form information and filtering the users input accordingly. It will also have to pass the data to the database. Finally if we are not using AJAX the script will redirect the user back to the page they came from so that they can see there newly added coment.

We will also need a script built into each page that will grab the comments that relate to that page and display them.

I am going to look up some SQL. Well you answer my questions and start to prepare the outlines of our scripts. Post back when you are ready to move forward.

Ok this will be a bit of a challenge for me, but that is because I try to avoid databases (SQL is not my fortay); but it will be a learning opportunity for me.

Just a little more planning before we begin. The database that you have does it already contain data similiar to the type of information we will be collecting? If so we could just give the comments a unique table in the database.

Have you already tested your server for PHP capability? Also how much understanding do you have of PHP?

Initially we are going to need a place to store the data. = In the database, either a seperate one or a unique table.

We will need to build a comment form to collect the information from the user.

We will have to build a php script that will be responsible for catching the form information and filtering the users input accordingly. It will also have to pass the data to the database. Finally if we are not using AJAX the script will redirect the user back to the page they came from so that they can see there newly added coment.

We will also need a script built into each page that will grab the comments that relate to that page and display them.

I am going to look up some SQL. Well you answer my questions and start to prepare the outlines of our scripts. Post back when you are ready to move forward.

 

Yes I would love to store my data in a sql data base. I can create a new data base for this script, just command me then i will do it. PHP is compatible with my web server. I do not know about PHP stuffs but I can do what you tell me :) I know html and css :) You know php so tell me the best thing to do then I can follow it :) I hope you visited my link witch i sent you. All right I'm ready.

 

 

 

 

 

 

OK go ahead and create the new MySQL database. Lets begin with a comment form. You said you understand html, so you should be able to make the form. Show me what you come up with make sure that for now the form method is POST and that the action is the name of the script that you want to proccess the comments, like "comment_post.php".

I did visit you link and I see how his comments work, but you can make the html side so that it will look just like that. To make your comments like his at a minimum you will need to collect the users name and there comment but of course you can collect extra information such as a email. This helps for validation.

 

We are going to need to plan a database structure, this could be very simple but it could be more complex to allow for easy expansion latter. Which would you perfer and how much do you know about databases and SQL?

This is my html form. Hope it is correct :) I do not know about lot of sql databases but I know how to create data bases and how to create tables from a code. Ok what we do next sir ? :) 

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Comment form</title>

 

</head>

 

<body>

 

 

<form method="post" action="comment_post.php">

 

<p>Name: <br />

<input type="text" name="visitor" size="35" />

<br /><br />

Email:<br />

<input type="text" name="visitormail" size="35" />

<br /> <br />

Web site:<br />

<input type="text" name="visitormail" size="35" />

<br /> <br />

Comment:

<br />

<textarea name="notes" rows="7" cols="45"></textarea>

 

<br />

<input name="submit" type="submit" value="Submit" />

 

</form >

 

 

 

 

 

</body>

</html>

 

Everything is good except that you named your website input box as visitormail.

OK next we will build comment_post.php .

<?php
if($_POST['submit']==='Submit') {
  $name=htmlspecialchars($_POST['visitor']); // we use htmlspecialchars to prevent some one from injecting content.
  $email=htmlspecialchars($_POST['visitormail']); //We could also use the filter_var() here if you want more precision.
  $url=htmlspecialchars($_POST['url']);
  $notes=htmlspecialchars($_POST['notes']);
  echo 'Let test to make sure the data is being passed.'."\n";
  echo 'name='.$name.' email='.$email.' url='.$url."\n".'notes='.$notes;
}
else { echo 'Nothing was Posted!'; }// we will change this to be more helpful later.
?>

We will need the name of the database and the table that you have created for the comments. You can keep the name of the database to yourself if you would like. Also do not at any point during these posts reveal your password.

Once you are able to successfully see the varibles getting passed well proceed to the next step. Also try to take a moment to understand why I used the code that I did.

http://php.net/manual/en/function.htmlspecialchars.php

http://php.net/manual/en/function.filter-var.php

http://php.net/manual/en/reserved.variables.post.php

Thank you very much for your comment_post.php :) Yes that was my mistake. I corrected it

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Comment form</title>

 

</head>

 

<body>

 

 

<form method="post" action="comment_post.php">

 

<p>Name: <br />

<input type="text" name="visitor" size="35" id="visitor" />

<br /><br />

Email:<br />

<input type="text" name="visitoremail" size="35" id="visitoremail" />

<br />

<br />

Web site:<br />

<input type="text" name="url" size="35" id="url" />

<br />

<br />

Comment:

<br />

<textarea name="notes" rows="7" cols="45" id="notes"></textarea>

 

<br />

<input name="submit" type="submit" value="Submit" />

 

</form >

 

 

 

 

 

</body>

</html>

 

 

I created the database and comment_post.php:)

 

Ok How many fields you want me to create ? 4 ?

 

What is the next step WolfRage ?

 

 

 

To allow you to move forward look up the SQL for simple insert into a single table. We will use that SQL statement to craft our php prepared SQL statement. Also did you test out the form with the script and did it echo all of the variables as expected?

Really Thanks, Gizmola! SOrry having a busy day at work today. When I get home I will work to put together the rest of your scripts so that you can implement your comments. Of course any other interested parties could actively contribute before then... giz... mola... lol

Our SQl statement for inserting the information in your data base will look like this:

<?php
$mysqli = new mysqli('dataBaseHost', 'userName', 'userPassword', 'userDataBase');
if ($mysqli->connect_error) { die('Connect Error ('.$mysqli->connect_errno .') '.$mysqli->connect_error); }
//Of course there are much better was to handle this error instead of dying but for now.
$sql='INSERT INTO table_name (visitor, visitoremail, url, notes) VALUES ('.$mysqli->real_escape_string($visitor).', '.$mysqli->real_escape_string($visitoremail).', '.$mysqli->real_escape_string($url).', '.$mysqli->real_escape_string($notes).')';
$mysqli->query($sql);
$mysqli->close();
?>

More to come...

All together the script will look something like this, minus your changes for your database information and the table name.

<?php
if($_POST['submit']==='Submit') {
  $name=htmlspecialchars(trim($_POST['visitor'])); // we use htmlspecialchars to prevent some one from injecting content.
  $email=htmlspecialchars(trim($_POST['visitormail'])); //We could also use the filter_var() here if you want more precision.
  $url=htmlspecialchars(trim($_POST['url']));
  $notes=htmlspecialchars(trim($_POST['notes']));
  if(empty($name)&&empty($email)&&empty($url)&&empty($notes)) {
    $mysqli = new mysqli('dataBaseHost', 'userName', 'userPassword', 'userDataBase');
    if ($mysqli->connect_error) { die('Connect Error ('.$mysqli->connect_errno .') '.$mysqli->connect_error); }
    //Of course there are much better was to handle this error instead of dying but for now.
    $sql='INSERT INTO table_name (visitor, visitoremail, url, notes) VALUES ('.$mysqli->real_escape_string($visitor).', '.$mysqli->real_escape_string($visitoremail).', '.$mysqli->real_escape_string($url).', '.$mysqli->real_escape_string($notes).')';
    $mysqli->query($sql);
    $mysqli->close();
  }
  else { echo 'Nothing was Posted!'; }// we will change this to be more helpful later.
  header('Location: '.$_SERVER['HTTP_REFERER']); //HTTP_REFERER may not always be set by the users browser!
}
else { echo 'Nothing was Posted!'; }// we will change this to be more helpful later.
?>

Next we will work on the script to extract these entries from your database; and then display them on your page.

For all of those critics out there:

Before we take this live we will need to make the input script more complex to include at the minimum a page column so that comments can be stored in the same table for multiple pages. We should also add a time stamp column which can be done with either PHP or MySQL.

Yes it echo like this

 

Let test to make sure the data is being passed. name=Bob email= url=www.testing.com notes=This is testing.

 

and I created the table and this is table code

 

 

CREATE TABLE  `mydatabase`.`comment` (

`commentid` INT NOT NULL AUTO_INCREMENT ,

`time` TIMESTAMP NOT NULL ,

`name` VARCHAR( 40 ) NOT NULL ,

`email` VARCHAR( 50 ) NOT NULL ,

`url` VARCHAR( 50 ) NOT NULL ,

`notes` VARCHAR( 500 ) NOT NULL ,

PRIMARY KEY (  `commentid` ) ,

UNIQUE (

`commentid`

)

) ENGINE = MYISAM

 

 

 

 

Hope this is correct :) and I did updated the comment_post.php as well.

Waiting for your reply.

 

 

this thread was never solved.  I'll try to solve it now.  I'm just gonna do a quick script because I cant sleep.  I wont look over it too much so if any one see's error correct them please.

 

This code is very similar to the code I use on my site.  I assume you have a login you'll need to check that the user is logged in before running this script however your using that.

 

 

 

Make the database look like this

Name = Comments
Fields:  (do not include these within name)
1. id (this is primary and auto_inc INT 32)
2. title (VARCHAR 32)
3. msg (LONGTEXT)
4. poster (VARCHAR 32)
5. replyid (INT 32)

 

I have no idea what you want them to be commenting on.  I'm going to assume its a story under a different table named "stories"

 

<?php
//story.php
/* The following is the page where you show the story.
This code may be different than yours but the important part is that we find the id from the stories table.
I'm also assuming that you've already forwarded to a page showing a specific story using its id and you've put the id in the address bar using a GET form. */

/*
we clicked on this link to get us to story.php

echo "<a href=story.php?id=$id>Man gets mauled by tiger!</a>";

*/


//Just getting information from the address bar
$id = $_GET['id'];
$action = $_GET['action'];

                                               //getting story info

                                                $query  = "SELECT * FROM stories where id = $id " ;
					$result = mysql_query($query);		

					while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$id = $row['id'];
					$title = $row['title'];
					$body = $row['body'];

                                                      //printing the story
                                                      echo "$title<br />";
                                                      echo "$body<br />";
                                                      echo "Posted by: $poster";


					}

                                                /*Getting comment info and printing all comments whos replyid is the same as the stories id */
                                                $query  = "SELECT * FROM comments where replyid = $id " ;
					$result = mysql_query($query);		

					while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
					$commentid = $row['id'];
					$title = $row['title'];
					$msg = $row['msg'];
                                                $poster = $row['poster'];
                                                $replyid = $row['poster'];
                                      


                                                      //Printing the comment
                                                      echo "$title<br />";
                                                      echo "$msg<br />";
                                                      echo "<small>Posted by: $poster </small><br /><br />";


					}





if ($action == 1) {
     /*here we are inserting a new comment because $action == 1 */

//setting post information in variables

$poster = $_POST['poster'];
$title = $_POST['title'];
$comment = $_POST['comment'];
$replyid = $_GET['id'];  //here we use the GET id because we want the replyid to be the same as the stories id

//allow apostrophes ;]
$poster = mysql_real_escape_string($poster);
$title = myql_real_escape_string($title);
$comment = mysql_real_escape_string($comment); 

if ($poster == '') {
     echo "You didn't leave a name!";
     kill();
}
if ( $title == '' ) {
     echo "You didn't title your comment!";
     kill();
}
if ( $comment == '' ) {
     echo "You left the comment space blank";
     kill();
}


$query = mysql_query(" INSERT INTO `YOUR_DATABASE_NAME`.`comments` (`id` , `poster` , `title` , `msg`, `replyid` ) VALUES ( NULL , '$poster', '$title', '$comment', '$replyid'; )") or die('Could not insert data because '.mysql_error()) ;

     echo "Your comment has been posted!";

    exit();  //kill the script so the form wont show because they just commented!  maybe hinder a little spam
}


                          

                            echo "<form action=story.php?action=1&id=$id method=POST>";
                            echo "Name:<input type=text MAXLENGTH=20 name=poster><br />";
                            echo "Title:<input type=text MAXLENGTH=20 name=title><br />";
                            echo "Comment:<br />";
                            echo "<textarea name=comment rows=10 cols=20>Type your comment here.</textarea><br />";
                            echo "<input type=submit value=Comment>";
                            echo "</form>";
                               



?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.