curbsy Posted October 25, 2005 Share Posted October 25, 2005 Yes, the topic of the day is: Comments! I'm trying (but failing miserably) to set up a comment system on my website. Does anybody have experience with this? Link to comment Share on other sites More sharing options...
actionsketch Posted October 25, 2005 Share Posted October 25, 2005 yes. There's tons of ways to do it, I suggest you find a premade package... maybe something like a blog... the simplest one you can find that does what you want and then start reading through it and trying to understand how they did it. Link to comment Share on other sites More sharing options...
MadPotato Posted October 25, 2005 Share Posted October 25, 2005 Well, maybe your doubt is a doubt of more users. The tutorials teach it, but i'll show a simple code of a GuestBook...first, create a dabatase with the name 'simplegb', and run this SQL to creat the structure: CREATE TABLE posts ( id int(11) NOT NULL auto_increment, userName varchar(25) NOT NULL default '', comment text NOT NULL, KEY id (id)) TYPE=MyISAM; Then, here goes the 3 files of the system. The first, index.php will show all the comments and display a link to post new ones.save this content as "index.php" <!-- Simple Guestbook System - By Bruno Daniel "MadPotato" --><!-- madpotato@gmail.com --><html><body><?//Connect to the DB $oConDB = mysql_connect("127.0.0.1","root","");//Select your DataBasemysql_select_db("simplegb",$oConDB);//Get all the comments...$cSQL = "SELECT * FROM posts ORDER BY id";$oRS = mysql_query($cSQL,$oConDB);//...and print them in a tablewhile($aRow = mysql_fetch_assoc($oRS)) { //PS.: each time 'mysql_fetch_assoc' is used, it takes a row from the resource // and puts it in an associative array echo $aRow["comment"]."<br />"; echo "<small> By: ".$aRow["userName"]."</small><br /><hr>";}//close database connection (very important! :P)mysql_close($oConDB);?><br /><br /><a href="post.php"> Post </a></body></html> The second is a form to be used by the users to post:save this content as "post.php" <html><body><form action="execute.php" method="post"> Your Name:<br /> <input name="userName" id="userName" type="text" size="35" maxlength="25" /><br /><br /> Your Comment:<br /> <textarea name="comment" id="comment" rows="10" cols="35"></textarea><br /><br /> <input type="submit" /></form></body></html> and the last one will insert the new post in the database:save this content as "execute.php" <html><body><?//Connect to the DB $oConDB = mysql_connect("127.0.0.1","rot","");//Select your DataBasemysql_select_db("SimpleGB",$oConDB);//insert new comment$cSQL = "INSERT INTO posts (userName,comment) VALUES ('".$_POST["userName"]."','".$_POST["comment"]."')";mysql_query($cSQL,$oConDB);//small Javascript Code to redirect user to indexecho "<script type=\"text/javascript\"> location.href='index.php' </script>";//close database connection (very important! :P)mysql_close($oConDB);?></body></html> Please notice that the code isn't optimized since its an example writen as easier to understand as possible. I've included lots of comments in the PHP functions. Hope it helped somebody. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now