Creating a simple PHP forum tutorial

Learn to create forum step by step, in this tutorials use only simple php code but powerful.

STEP6: Create file add_answer.php

############### Code


// Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum_answer"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get value of id that sent from hidden field

// Find highest answer number.
$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id='$id'";

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
else {
$Max_id = 1;

// get values that sent from form

$datetime=date("d/m/y H:i:s"); // create date and time

// Insert answer

$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";

echo "Successful<BR>";
echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";

// If added new answer, add value +1 in reply column

$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";

else {
echo "ERROR";

// Close connection


1 | 2 | PAGE 3

Similar Post

Test php mail function on your localhost

When you code php mail function you can't test it on your localhost you have to upload and test it on your hosting but now you can test php mail function on your localhost using Argosft mail server.

Automatic refresh webpage / Set time to redirect

When you need your web page automatic refresh in 5 second or any second, use this meta tag. it's a simple code, put it between HEAD tag in your web page.