Code for writing the comment to the DB

Now create a new page called ‘addcomment.php’

<?php
$conn = mysql_connect('localhost', 'username', 'password');
mysql_select_db('yourdb');

$comdate = $_POST['comdate'];
$articleid = $_POST['articleid'];
$comsubject = htmlspecialchars(addslashes($_POST['comsubject']));
$comuser = htmlspecialchars(addslashes($_POST['comuser']));
$comtext = htmlspecialchars(addslashes($_POST['comtext']));

//write the query
$sql = "insert into comments(comdate, articleid, comsubject, comuser, comtext)";
$sql .= " values ('" . $comdate . "', " . $articleid . ", '" . $comsubject . "', '" . $comuser . "', '" . $comtext . "');";

if (!@mysql_query($sql)) {
  echo ('We are unable to register your comment, sorry');
} else {
  echo ("Your comment was added successfully<br>");
  echo ("<A HREF="article.php">Back to Articles</A>");
}

mysql_close($conn);
?> 

One thought on “Code for writing the comment to the DB”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.