Create a comment box in PHP and MySql for each ID

2019-09-14 22:39发布

问题:

I am doing a site where people can leave comments on all the pictures I have photographed. Every photo has it’s own page; Id=1 Id=2 etc.

I would like a comment box where my visitors can add and see other comments already posted.

Any tips or example code would be very appreciated.

回答1:

I assume you have some basic knowledge about MySQL, PHP and using MySQL with PHP. Do you already have a database table defined?

Anyway, for the comments, assuming they can be written anonymously, I would create a table comment as such:

`id` INT AUTO_INCREMENT,
`image_id` INT NOT NULL,
`content` VARCHAR(1024) NOT NULL,
`timestamp` TIMESTAMP NOT NULL DEFAULT NOW(),
PRIMARY KEY (`id`)

Create a simple form that will send you to a php page which inserts the entered data into the database.

<form name="comment" action="addcomment.php" method="post">
  <input type="hidden" id="image_id" value="$image_id" />
  <textarea id="content"></textarea>
  <input type="submit" />
</form>

The $image_id should be replaced in your php script by the ID of the image that is being commented on.

The database entry in addcomment.php should contain something similar to this:

<?php
$image_id = $_POST['image_id'];
$content = $_POST['content'];

mysql_query('INSERT INTO `comment` (`image_id`, `content`) VALUES('.$image_id.', "'.$content.'");
?>

Note: those are only bare bone hints that will both look bad and be insecure, but they should help you getting started with this...



回答2:

No need to code , you can register a account on http://disqus.com and add some javascript to your site, that's all. See document on disqus.com for more information.



回答3:

If you are going to put one picture per page, rather than coding why not use some blogging software like wordpress?