MySQL

Creating a Simple Discussion Forum with PHP and Mysql

  • Oleh admin
  • 12 menit baca
  • Belum ada komentar
First step :  Create Database with the name discuss_db.
  
Second step : Download from this Link To Download Database.  And import to your database.
Third step : Create File with the name topic.php in your PHP text editor, and copy this script :
<head>
<html>
<title>Create Topic</title>
</head>
<body>
<table width=”400″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<form id=”form1″ name=”form1″ method=”post” action=”add_topic.php”>
<td>
<table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″ bgcolor=”#FFFFFF”>
<tr>
<td colspan=”3″ bgcolor=”#E6E6E6″><strong>Create New Topic</strong> </td>
</tr>
<tr>
<td width=”14%”><strong>Topic</strong></td>
<td width=”2%”>:</td>
<td width=”84%”><input name=”topic” type=”text” id=”topic” size=”50″ /></td>
</tr><tr>
<td valign=”top”><strong>Detail</strong></td>
<td valign=”top”>:</td>
<td><textarea name=”detail” cols=”50″ rows=”3″ id=”detail”></textarea></td>
</tr>
<tr><td><strong>Name</strong></td>
<td>:</td>
<td><input name=”name” type=”text” id=”name” size=”50″ /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name=”email” type=”text” id=”email” size=”50″ /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type=”submit” name=”Submit” value=”Submit” />
<input type=”reset” name=”Submit2″ value=”Reset” /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<p align=”center”><a href=”main.php”>Cancel And Back to Main View</a></p>
</body>
</html>

Fourth step : Create File with the name add_topic.php in your PHP text editor, and copy this script :

    <html>
<head>
<title>Add Topic</title>
</head>
<body>
<?php
$host=”localhost”; // Host name
$username=”root”; // Mysql username
$password=””; // Mysql password
$db_name=”discuss_db”; // Database name
$tbl_name=”question”; // Table name// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);// get data that sent from form
$q_topic=$_POST[‘topic’];
$q_detail=$_POST[‘detail’];
$q_name=$_POST[‘name’];
$q_email=$_POST[‘email’];
$q_datetime=date(“d/m/y h:i:s”); //create date time
$sql=”INSERT INTO $tbl_name(q_topic, q_detail, q_name, q_email, q_datetime)VALUES(‘$q_topic’, ‘$q_detail’ , ‘$q_name’, ‘$q_email’, ‘$q_datetime’)”;
$result=mysql_query($sql);
if($result){
echo “Successfully<br \>”;
echo “<a href=main.php>View your topic</a>”;
}
else {
echo “ERROR”;
}
mysql_close();
?>
<p align=”center”><a href=”main.php”>View Disscus Topic</a></p>
</body>
</html>
Fifth step : Create File with the name main.php in your PHP text editor, and copy this script :
    <html>
<head>
<title>Main Page</title>
</head>
<body>
<?php
$host=”localhost”; // Host name
$username=”root”; // Mysql username
$password=””; // Mysql password
$db_name=”discuss_db”; // Database name
$tbl_name=”question”; // 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”);
$sql=”SELECT * FROM $tbl_name ORDER BY q_id DESC”;// OREDER BY q_id DESC is order result by descending
$result=mysql_query($sql);
?>
<table width=”90%” border=”0″ align=”center” cellpadding=”3″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<td width=”6%” align=”center” bgcolor=”#E6E6E6″><strong>#</strong></td>
<td width=”53%” align=”center” bgcolor=”#E6E6E6″><strong>Topic</strong></td>
<td width=”15%” align=”center” bgcolor=”#E6E6E6″><strong>Views</strong></td>
<td width=”13%” align=”center” bgcolor=”#E6E6E6″><strong>Replies</strong></td>
<td width=”13%” align=”center” bgcolor=”#E6E6E6″><strong>Date/Time</strong></td>
</tr>
<?php
$number=1;
?>
<?php
while($rows=mysql_fetch_array($result)){  // Start looping table row
?>
<tr>
<td bgcolor=”#FFFFFF”><?php echo $number; ?></td>
<td bgcolor=”#FFFFFF”><a href=”answer.php?id=<?php echo $rows[‘q_id’]; ?>”>
<?php echo $rows[‘q_topic’]; ?></a><br \></td>
<td align=”center” bgcolor=”#FFFFFF”><?php echo $rows[‘q_view’]; ?></td>
<td align=”center” bgcolor=”#FFFFFF”><?php echo $rows[‘q_reply’]; ?></td>
<td align=”center” bgcolor=”#FFFFFF”><?php echo $rows[‘q_datetime’]; ?></td>
<?php
$number=$number+1;
?>
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan=”5″ align=”right” bgcolor=”#E6E6E6″><a href=”topic.php”><strong>Create New Topic</strong> </a></td>
</tr>
</table>
</body></html>

Sixth step : Create File with the name answer.php in your PHP text editor, and copy this script :

    <html>
<head>
<title>Giving answer </title>
</head>
<body>
<?php
$host=”localhost”; // Host name
$username=”root”; // Mysql username
$password=””; // Mysql password
$db_name=”discuss_db”; // Database name
$tbl_name=”question”; // 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 address bar
$id=$_GET[‘id’];
$sql=”SELECT * FROM $tbl_name WHERE q_id=’$id'”;
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width=”400″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<td><table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″ bordercolor=”1″ bgcolor=”#FFFFFF”>
<tr>
<td bgcolor=”#F8F7F1″><strong><? echo $rows[‘q_topic’]; ?></strong></td>
</tr>
<tr>
<td bgcolor=”#F8F7F1″><? echo $rows[‘q_detail’]; ?></td>
</tr>
<tr>
<td bgcolor=”#F8F7F1″><strong>By :</strong> <? echo $rows[‘q_name’]; ?>
<strong>Email : </strong><? echo $rows[‘q_email’];?></td>
</tr><tr>
<td bgcolor=”#F8F7F1″><strong>Date/time : </strong><? echo $rows[‘q_datetime’];
?></td>
</tr>
</table></td>
</tr>
</table>
<br \>
<?php
$tbl_name2=”answer”; // Switch to table “answer”
$sql2=”SELECT * FROM $tbl_name2 WHERE q_id=’$id'”;
$result2=mysql_query($sql2);
$number=1;
while($rows=mysql_fetch_array($result2)){
?>
<table width=”400″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<td><table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″ bgcolor=”#FFFFFF”>
<tr>
<td width=”18%” bgcolor=”#F8F7F1″><strong>Number</strong></td>
<td width=”5%” bgcolor=”#F8F7F1″>:</td>
<td width=”77%” bgcolor=”#F8F7F1″><? echo $number; ?></td>
</tr>
<tr>
<td width=”18%” bgcolor=”#F8F7F1″><strong>Name</strong></td>
<td width=”5%” bgcolor=”#F8F7F1″>:</td>
<td width=”77%” bgcolor=”#F8F7F1″><? echo $rows[‘a_name’]; ?></td>
</tr>
<tr>
<td bg
color=”#F8F7F1″><strong>Email</strong></td>
<td bgcolor=”#F8F7F1″>:</td>
<td bgcolor=”#F8F7F1″><? echo $rows[‘a_email’]; ?></td>
</tr>
<tr>
<td bgcolor=”#F8F7F1″><strong>Answer</strong></td>
<td bgcolor=”#F8F7F1″>:</td>
<td bgcolor=”#F8F7F1″><? echo $rows[‘a_answer’]; ?></td>
</tr>
<tr>
<td bgcolor=”#F8F7F1″><strong>Date/Time</strong></td>
<td bgcolor=”#F8F7F1″>:</td>
<td bgcolor=”#F8F7F1″><? echo $rows[‘a_datetime’]; ?></td>
</tr></table></td>
</tr>
</table><br \>
<?php
$number=$number+1;
?>
<?php
}
$sql3=”SELECT q_view FROM $tbl_name WHERE q_id=’$id'”;
$result3=mysql_query($sql3);
$rows=mysql_fetch_array($result3);
$view=$rows[‘q_view’];
// if have no counter value set counter = 1
if(empty($q_view)){
$q_view=1;
$sql4=”INSERT INTO $tbl_name(q_view) VALUES(‘$q_view’) WHERE q_id=’$id'”;
$result4=mysql_query($sql4);
}
// count more value
$addview=$q_view+1;
$sql5=”update $tbl_name set q_view=’$addview’ WHERE q_id=’$id'”;
$result5=mysql_query($sql5);
mysql_close();
?>
<br \>
<table width=”400″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<form name=”form1″ method=”post” action=”add_answer.php”>
<td>
<table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″ bgcolor=”#FFFFFF”>
<tr>
<td width=”18%”><strong>Name</strong></td>
<td width=”3%”>:</td>
<td width=”79%”><input name=”a_name” type=”text” id=”a_name” size=”45″></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name=”a_email” type=”text” id=”a_email” size=”45″></td>
</tr>
<tr><td valign=”top”><strong>Answer</strong></td>
<td valign=”top”>:</td>
<td><textarea name=”a_answer” cols=”45″ rows=”3″ id=”a_answer”></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name=”id” type=”hidden” value=”<? echo $id; ?>”></td>
<td><input type=”submit” name=”Submit” value=”Submit”> <input type=”reset”name=”Submit2″ value=”Reset”></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<p align=”center”><a href=”main.php”>Cancel and Back To Home</a></p>
</body>
</html>

seventh step : Create File with the name add_answer.php in your PHP text editor, and copy this script :
    <html>
<head>
<title>Adding Answer</title>
</head>
<body>
<?php
$host=”localhost”; // Host name
$username=”root”; // Mysql username
$password=””; // Mysql password
$db_name=”discuss_db”; // Database name
$tbl_name=”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
$id=$_POST[‘id’];
// Find highest answer number.
$sql=”SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE q_id=’$id'”;
$result=mysql_query($sql);$rows=mysql_fetch_array($result);
// 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
$a_name=$_POST[‘a_name’];
$a_email=$_POST[‘a_email’];
$a_answer=$_POST[‘a_answer’];
$a_datetime=date(“d/m/y H:i:s”); // create date and time
// Insert answer
$sql2=”INSERT INTO $tbl_name(q_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES(‘$id’, ‘$Max_id’, ‘$a_name’, ‘$a_email’, ‘$a_answer’, ‘$a_datetime’)”;
$result2=mysql_query($sql2);
if($result2){
echo “Successfully<br \>”;
echo “<a href=’answer.php?id=”.$id.” ‘>View your answer</a>”;
// If added new answer, add value +1 in reply column
$tbl_name2=”question”;
$sql3=”UPDATE $tbl_name2 SET q_reply=’$Max_id’ WHERE q_id=’$id'”;
$result3=mysql_query($sql3);
}
else {
echo “ERROR”;
}
mysql_close();
?>
</body>
</html>

In the end try to call program using your Web Browser. Thanks

Bagikan

Tentang Penulis

admin

Butuh Bantuan Mengembangkan Website atau Aplikasi?

Diskusikan kebutuhan proyek Anda dan tentukan solusi yang paling sesuai.

0 comments on Creating a Simple Discussion Forum with PHP and Mysql

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *