I wrote a basic script that allows anybody to write a comment and post it. The comments are displayed under the post form and the number of displayed comments can be modified by the user.
I'm going to post the code because i'm kind of proud of writing it considering i'm still a PHP beginner. I'd appreciate any pointers on how to improve the code if any more experienced coders decide to have a look through.
connect.inc.php
<?php //connect to phpmyadmin mysql_connect('localhost','root',''); // select the database you want to work with. mysql_select_db('wall'); //add comment function add_comment($name, $comment) { $date = date("D j M Y (G:i:s)"); $name = htmlentities(mysql_real_escape_string($name)); $comment = htmlentities(mysql_real_escape_string($comment)); mysql_query ("INSERT INTO `wall` (`user_name`, `user_comment`, `date`) VALUES ('{$name}', '{$comment}', '{$date}')"); } ?>index.php
<?php include('connect.inc.php'); $display = 4; if (isset($_POST['numofposts'])) { $display = $_POST['numofposts']; } $errors = array(); if (isset($_POST['name'], $_POST['comment'])) { if (!empty($_POST['name']) && !empty($_POST['comment'])) { add_comment($_POST['name'], $_POST['comment']); } else { $errors[] = 'Please make sure you have entered your name and a comment.'; } } else { $errors[] = 'Please enter name and comment'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://wwww.w3.org/TR/xhtml1/DTD/dhtml1-strict.dtd"> <html xlmns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> Basic Wall System </title> <style> body{margin: 0px; padding:0px;} #container {width: 1000px; margin: 0px auto; text-align: center;} </style> </head> <body> <div id="container"> <?php foreach ($errors as $error) { echo $error; } if (empty($errors)) { echo 'Post Successful'; } ?> <form action="" method="POST"> <p>Name:</p> <p><input type="text" name="name" value="<?php echo $_POST['name']; ?>"/></p> <p>Comment:</p> <p><textarea name="comment" cols="50" rows="8" name="comment" value="<?php echo $_POST['comment']?>"></textarea></p> <p>Number of Posts to Display: <input style="width: 14px;" type="text" name="numofposts" value="<?php echo $display; ?>" /> <p><input type="submit" value="Submit Comment" /> </p> </form> <hr /> <?php $query = "SELECT `post_id`, `user_name`, `user_comment`, `date` FROM `wall` ORDER BY `date` DESC LIMIT 0, {$display}"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { echo '<p><strong>Date: </strong>'.$row['date'].'</p>'; echo '<p><strong>Name: </strong>'.$row['user_name'].'</p>'; echo '<p><strong>Comment: </strong>'.$row['user_comment'].'</p>'; echo '<hr />'; } ?> </div> </body> </html>ps: I'd appreciate any other basic script ideas I can try and code for practice purposes.