Either I'm making this harder than what it should be or I'm just not understanding it but I'm still having issues with thread replying. To make things worse I've confused myself even more with getting the database setup and even showing the topics on the index page. My code is below and I really would appreciate some help with this. This is meant to be a small post and reply thing, not a real, "bulletin board". There is no need for sessions or login features or what-not. Thanks for any help you all can provide. 🙂
In my database I have two tables:
Topics:
ID INT PRIMARY KEY AUTO_INCREMENT
threadtitle TEXT
threadauthor TEXT
threaddate DATETIME
and
Posts
IDpost INT PRIMARY KEY AUTO_INCREMENT
threadid INT
threadtitle TEXT
threadauthor TEXT
threadmessage TEXT
threaddate TEXT
I'm sure my database setup is wrong, so that's one problem. Another is getting them to work together. I belive that if I can get the database setup correct and figure out how to tie the postid in with the threadid, I'll be OK. However, I can't make it to that point yet and I haven't even added in the reply function yet. Again, thanks for helping! 🙂
<?php
require('db.php');
function displayThreads() {
$threads = mysql_query("SELECT * FROM topics");
if(!$threads) {
echo("Cannot retrieve topics.");
}
echo("Topics:<br><br>");
while($threadsrow = mysql_fetch_array($threads)) {
$id = $threadsrow['ID'];
$threadtitle = $threadsrow['threadtitle'];
$threadauthor = $threadsrow['threadauthor'];
$threaddate = $threadsrow['threaddate'];
echo("<a href=\"$_SERVER[PHP_SELF]?thread=$threadid\">$threadtitle</a><br>");
echo("<b>Posted by:</b> $threadauthor<br>");
echo("<b>On:</b> $threaddate<br><br>");
}
}
function displayThread($thread) {
$disptopics = mysql_query("SELECT * FROM topics WHERE ID = $thread");
$dispthread = mysql_query("SELECT * FROM posts");
if($post = mysql_fetch_array($disptopics,$dispthread)) {
$threadid = $post['ID'];
$postid = $post['threadid'];
$threadtitle = $post['threadtitle'];
$threadauthor = $post['threadauthor'];
$threadmessage = $post['threadmessage'];
$threaddate = $post['date'];
$postid = $post['IDpost'];
echo("$threadtitle<br>");
echo("<b>Posted by:</b> $threadauthor<br>");
echo("<b>On:</b> $threaddate<br><br>");
echo("--------------------------------------------------------------------------------------------------------<br>");
echo("$threadmessage");
echo("--------------------------------------------------------------------------------------------------------<br><br>");
echo("<a href='index.php'>Home</a><br>");
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="<?php echo("$PHP_SELF")?>">
Thread title: <input type="text" name="threadtitle"><br>
Author: <input type="text" name="threadauthor"><br>
Message: <textarea name="threadmessage" rows=6 cols=40></textarea><br>
<input type="submit" name="submit" value="Submit">
<?php
if($_POST['submit'] == "Submit") {
$topics = mysql_query("INSERT INTO topics SET
threadtitle = '$threadtitle',
threadauthor = '$threadauthor',
threaddate = NOW()");
$posts = mysql_query("INSERT INTO posts SET
threadtitle = '$threadtitle',
threadauthor = '$threadauthor',
threaddate = NOW()");
if(!$topics OR !$posts) {
echo("Error.");
} else {
echo("Topic added.");
}
}
if($_GET['thread']) {
displayThread($_GET['thread']);
} else {
displayThreads();
}
?>