<?php
# Define values host,dbname,username and password as $s before the try{} statement
   try {
     $dbh = new PDO("mysql:host=$host;dbname=$dbname", $username,$password);
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   } catch (PDOException $e) {
     print ("Could not connect to server.\n");
   }
# Comments page
# Get the info and organize it in an array
$info = array(
"com"=>$_POST['comment'],
"name"=>$_POST['poster']);
# Create an array in case of errors
$errors = array();
# Check each entry
if(empty($info['com']))
      $errors[]="Invalid comment.";
if(empty($info['name']))
      $errors[]="Invalid username.";
# If there were errors tell them what went wrong and post a link to go back to the form page
if($errors) {
      echo "There were errors with your post: ";
      echo "<ul><li>";
      echo implode("</li><li>",$errors);
      echo "</li></ul>";
      echo "<a href='formPage'>Go back</a>";
      exit;
}
# SQL query that will insert data into database
$sth = $dbh->prepare("INSERT INTO comments(id,comment,poster,date) VALUES(0,?,?,now())");
$sth->execute(array($info['com'],$info['name']));
echo "Comment posted.";
?>