向数据库添加新记录可防止回显其他记录

时间:2015-04-28 02:30:25

标签: php mysql sql forum

您好我创建了一个论坛网站,其中登录的用户可以使用每个论坛主题上的表单创建主题,但是当我使用表单添加新记录时,它会阻止自身以及之前的主题显示。

我不知道为什么,并且会对任何帮助都很满意。 我的论坛页面显示了类别,主题(如果选择了类别)和回复(如果选择了主题)。这是我的forum.php页面的代码

    <div id="midclmn">
    <?php

    If (isset($_GET['topic'])){

        // Show topic & replies

        $queryreply = "SELECT a.reply_id,a.reply_text, a.reply_date, b.topic_title, b.topic_date,c.username AS reply_user, (SELECT username FROM users 
        WHERE user_id=b.user_id) AS topic_creator FROM forum_replies a 
        LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
        LEFT JOIN users c ON a.user_id=c.user_id

                WHERE a.topic_id = '".$_GET['topic']."'    ";
                       $result = mysql_query($queryreply) or die (mysql_error());
                        $row = mysql_fetch_array($result);
                         if(empty($row['reply_id'])){
                        echo "No replies have been posted in this Topic, be the first to have your say using form below.";} ?>



        <table id="categorytable">

                <tr><td><?php echo '<b>'.$row['topic_title'].'</b>';?></b><br></td></tr>
                <tr><td><?php echo 'Topic published by '.$row['topic_creator'].' - ( '.$row['topic_date'].' )'.'';?><br><br></td></tr>
                <tr><td><?php $row['reply_user'].' Replied with: ';?><br></td></tr>
                <tr><td><?php echo $row['reply_text'].'<br><br><i>Published:  '.$row['reply_date'].' by    '.$row['reply_user'].'</i>';?></td></tr>




            <?php

                while ($row = mysql_fetch_array($result)){ ?>

                <tr><td><?php $row['reply_user'].' Replied with: ';?><br></td></tr>
                <tr><td><?php echo $row['reply_text'].'<br><br><i>Published:  '.$row['reply_date'].' by    '.$row['reply_user'].'</i>';?></td></tr>


            <?php   
            }
?>
            </table>








        <?php

        }elseif (isset($_GET['cat'])){
        // Show topics in that category


        $querytopic = "SELECT topic_id, topic_title,topic_description, topic_date, category_id FROM forum_topics WHERE category_id = '".$_GET['cat']."'";
        $result = mysql_query($querytopic) or die (mysql_error());
        $row = mysql_fetch_array($result); 
         if(empty($row['topic_id'])){
            echo "No topics have been posted in this category, be the first to submit a topic using form below."; ?>
        <table id="categorytable">
                <tr><td><?php echo '<a href="forum.php?topic='.$row['topic_id'].'&cat='.$row['category_id'].'"><b>'.$row['topic_title'].'</b></a>';?></td></tr>
                <tr><td><?php echo $row['topic_description'];?><br><br></td></tr>
                <tr><td><?php echo "<b>Date Posted:</b>   ".$row['topic_date'];?><br><br></td></tr>
            <?php

                while ($row = mysql_fetch_array($result)){ ?>
                    <tr><td><?php echo '<a href="forum.php?topic='.$row['topic_id'].'&cat='.$row['category_id'].'"><br>'.$row['topic_title'].'</b></a>';?></td></tr>
                    <tr><td><?php echo $row['topic_description'];?><br><br></td></tr>
                    <tr><td><?php echo "<b>Date Posted:</b>   ".$row['topic_date'];?><br><br></td></tr>


            <?php   
            }} ?></table><?php
                if($_SESSION['loggedin'] === true){
                    $userid = $_SESSION['id'];
                    $catid = $_GET['cat'];

                    ?>

            <br>
            <form method="post" action="topic_process.php" id="topicform">
                <h3>Add New Topic</h3>
            <input type="hidden" id="catid" name="catid"  value=<?php echo $catid?> > 
            <input type="hidden" id="userid"    name="userid"  value=<?php echo $userid ?>>  
            <label for="topictitle">Topic Title:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <textarea rows="2" cols="80"  id="topictitle" name="topictitle"  required ></textarea>
                <br><br>
            <label for="topicdescription">Topic Description:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <textarea  rows="10" cols="80" id="topicdescription" name="topicdescription"   required ></textarea>
            <input type= "hidden" id="topicdate" name="topicdate" value=<?php echo ''.date('Y-m-d').'' ?>>

                <br><br>
                <input type="submit" name="topicsubmit" id="topicsubmit" value="Create Topic">


            </form>

<?php
                }
?>



        <?php   
        }else{ 
        //Just display the list of categories
        $querycategory = "SELECT category_id, category_title,category_description FROM forum_category";
        $result = mysql_query($querycategory) or die (mysql_error());
        $row = mysql_fetch_array($result); ?>

        <table id="categorytable">
                <tr><td><?php echo '<a href="forum.php?cat='.$row['category_id'].'">'.$row['category_title'].'</a>';?></td></tr>
                <tr><td><?php echo $row['category_description'];?><br><br></td></tr>
            <?php

                while ($row = mysql_fetch_array($result)){ ?>
                    <tr><td><?php echo '<a href="forum.php?cat='.$row['category_id'].'">'.$row['category_title'].'</a>';?></td></tr>
                    <tr><td><?php echo $row['category_description'];?><br><br></td></tr>


            <?php   
            }
                ?>
            </table>
<?php   }


 ?>

</div>

这是流程页面         

session_start();
include "includes/connection.php";

echo $_POST['topictitle'];
echo $_POST['topicdescription'];
echo $_POST['userid'];
echo $_POST['catid'];




$query = "INSERT INTO forum_topics
(
    category_id,
    user_id,
    topic_title,
    topic_description,
    topic_date
    )
VALUES
    (
    '".$_POST['catid']."',
    '".$_POST['userid']."',
    '".$_POST['topictitle']."',
    '".$_POST['topicdescription']."',
    '".$_POST['topicdate']."'
    )";     
mysql_query($query) or die (mysql_error());
header('Location: /TEST/forum.php?'.$_POST['catid'].'');


?>

我也在努力将topic_date记录为今天我在2015-04-28尝试日期('Y-m-d')的日期,但它保存为00-00-00。

我知道它有很多代码需要阅读,但我真的卡住了,不想错过任何东西,我也知道mysql_functions已被弃用但已被要求通过uni使用它们。

我使用PHPmyadmin作为数据库,我知道这是垃圾! 谢谢

编辑:我已经设置了页面,因此它显示了存储在数据库中的所有主题(topic_title,topic_description,user_id,topic_date)。在我向数据库添加一个新主题之前,这个工作正常,但之后包括新主题在内的所有主题都会消失,只留下页面上的表单。所有记录仍然存在于数据库中,删除新记录会带回页面上显示的所有主题。

0 个答案:

没有答案