It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

Adding comments in every page

Moderator: Malcolm

Adding comments in every page

Postby ghosh on Mon Jan 01, 2007 2:01 am

Dear frineds
I for quite some time I worked as a Photographer. Now I am building a Photography guide which in my website which others could use for learning. I have seen many such free Ebooks. There is some thing new that I want to do in my website, I want to add comments to every page. Where user would come, read and add comments.

Can any one tell me how should I go about :?: I am novice in PHP

Ghosh
ghosh
 
Posts: 7
Joined: Sat Dec 30, 2006 2:24 am

Postby Bliss on Mon Jan 01, 2007 11:08 pm

If you have a MySQL database, then this is quite simple.

First, run this script via PhpMyAdmin:
Code: Select all
CREATE TABLE `comments` (
`id` INT( 255 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NOT NULL ,
`comment` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM


connect.php: (fill in the data, make sure you name it connect.php)
Code: Select all
<?php
$host = "(enter host name)";
$username = "(enter username)";
$password = "(enter password)";
$database = "(enter database)";
mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
?>


Add this to the part where you want users to view/add comments:
Code: Select all
<html>
<?php
include 'connect.php';
$sql = mysql_query("SELECT * FROM comments ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
$name = $row['name'];
$comment = $row['comment']; ?>
<table width="200" border="0">
  <tr>
    <td width="61">Name:</td>
    <td width="123"><?php echo $name; ?></td>
  </tr>
  <tr>
    <td>Comment:</td>
    <td><?php echo $name; ?></td>
  </tr>
</table>
</br>
<?php } ?>
<p>&nbsp;</p>
<p>
  <?php
if (isset($_GET['name']) and isset($_GET['comment']) and $_GET['submit'] == "Submit") {
$name = addslashes($_GET['name']);
$comment = addslashes($_GET['comment']);
mysql_query("INSERT INTO comments (name, comment) VALUES ('$name', '$comment')") or die(mysql_error());
} else { ?>
</p>
<form name="form1" method="get" action="">
  <p>&nbsp;</p>
  <table width="257" border="0">
    <tr>
      <td width="79">Name:</td>
      <td width="105"><input type="text" name="name"></td>
    </tr>
    <tr>
      <td>Comment:</td>
      <td><label>
        <textarea name="comment" rows="6"></textarea>
      </label></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input type="submit" name="Submit" value="Submit">
      </label></td>
    </tr>
  </table>
</form>
<?php } ?>
</html>
Bliss
 
Posts: 9
Joined: Mon Jan 01, 2007 4:09 pm
Location: Toronto

Postby ghosh on Tue Jan 02, 2007 2:10 am

Hello Bliss,

Thanks a lot for that help. I do have My SQl. One thing one want to know about that script. Where those added messages will be shown? I hope after the articles.

Thanks
Ghosh
ghosh
 
Posts: 7
Joined: Sat Dec 30, 2006 2:24 am

Postby Bliss on Tue Jan 02, 2007 3:05 am

The first code needs to be put through your PhpMyAdmin. Or you could make a php page like this:
Code: Select all
<?php
include 'connect.php';
mysql_query("CREATE TABLE `comments` (
`id` INT( 255 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NOT NULL ,
`comment` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM"); ?>

After you make the page, run it on your server and then delete it right after.

The second code needs to be put in a separate file and you have to put in your MySQL information.

The third code should be placed after your photographs or wherever you'd like people to add comments.
Bliss
 
Posts: 9
Joined: Mon Jan 01, 2007 4:09 pm
Location: Toronto

Postby ghosh on Thu Jan 04, 2007 5:40 am

Thanks Bliss.

Ghosh
ghosh
 
Posts: 7
Joined: Sat Dec 30, 2006 2:24 am


Who is online

Users browsing this forum: No registered users and 0 guests