[PHP] COMMENT SYSTEM USING PHP
Hi all,
Hope it helps!
Am dropping this coz some are having problem with it, all procedures on how to do it is in the code, read well and edit well! 🙂
1 |
<?php <br /> //connect to your database, fillup the database details <br /> $dbhost = "";// Your database hostname <br /> $dbuser = "";// your database username <br /> $dbname = "";//Your database name <br /> $dbpwd = "";//Your database password, leave empty if your database don't have password <br /> $connect = mysql_connect($dbhost,$dbuser,$dbpwd); <br /> if(!$connect) <br /> { <br /> die('Could not connect to database due to the following reasons: '.mysql_error()); <br /> } <br /> mysql_select_db($dbname); <br /> ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br /> <html xmlns="http://www.w3.org/1999/xhtml"> <br /> <head> <br /> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <br /> <title>Untitled Document</title> <br /> </head> <br /> <body> <br /> <?php <br /> //checking if comment has been made <br /> if(isset($_POST['comment'])) <br /> { <br /> $comment = mysql_real_escape_string(trim($_POST['comment'])); <br /> if(!$comment) <br /> { <br /> die('Enter Comment'); <br /> } <br /> /*inserting comment into database, with the following columns in the table <br /> *id = which the comment id which will auto increase when ever comment is made <br /> *topic_id = Every comment must have a topic it is commenting for, so that the topic id from another table you created, you can hold it with a session or hidden form <br /> *reply = this is the main body of the reply message <br /> *uid = It must be a user that should made the comment and so from your users table you get the id column and hold it with a session, cookie or hidden form <br /> *date = Surely, it must have a date*/ <br /> //now inserting into database, using example of topic id = 1 and <br /> $date = date('d/m/Y'); <br /> mysql_query("insert into comment(topic_id,reply,uid,date)values('1','$comment','1','$date')") or die('Could not insert comment into database due to the following reasons: '.mysql_error()); <br /> } <br /> //show comment for a particular topic, assumming the topic id is 1 and user id is also 1 <br /> $query = mysql_query("select * from comment where topic_id='1'"); <br /> if(!$query) <br /> { <br /> die(mysql_error()); <br /> } <br /> while($row = mysql_fetch_array($query)) <br /> { <br /> $topic_id = $row['topic_id']; <br /> $uid = $row['uid']; <br /> $reply = $row['reply']; <br /> $date = $row['date']; <br /> ?><a href="">user<?=$uid?></a>: <?=$reply?><br/><?=$date?><br/> <br /> <?php } <br /> ?> <br /> <form action="" method="post"> <br /> <textarea placeholder="Reply" name="comment"></textarea> <br /> <input type="submit" value="Comment" /> <br /> </form> <br /> Created by <a href="http://2netlodge.com">2netlodge</a> <br /> </body> <br /> </html> <br /> |
Hope it helps!