php - 向论坛添加新主题

标签 php mysql

我正在尝试创建仅允许登录用户在论坛中创建新主题的代码。

我有一个forum.php、topic_process.php、reply_process.php页面。论坛已全部设置完毕,仅显示为登录用户发布新主题或新回复的表单,我将 forum.php 中的表单设置为 action="topic_process.php"。然而,我在 topic_process.php 页面上遇到了困难,这是我的代码,当我尝试发布主题时,它只是回显“您无法发布新主题”。

以下是论坛主题表的概述:

forumtopic_id, 
forumtopic_forumcategory_id,
forumtopic_title, 
forumtopic_text,
forumtopic_user_id, 
forumtopic_date, 

这是我在 topic_process.php 页面中的代码。 (请记住,我没有放入任何验证内容等,但在我可以将其发布到数据库后会这样做)提前致谢。

<?php
session_start();

include "includes/pagetop.php";


 $query  = "newtopic;";
$result = mysql_query($query);

if(!$result)
{

    echo 'You are not able to post a new topic, please try again later';
}
else
{


    $sql = "INSERT INTO forumreply
    (
                       formtopic_forumcategory_id,
                       forumtopic_title,
                       forumtopic_text,
                       forumtopic_user_id,
                       forumtopic_date
                       )
           VALUES('" . mysql_real_escape_string($_POST['topic_cat']) . "',
                       NOW(),
                       " . mysql_real_escape_string($_POST['topic_cat']) . ",
                       " . $_SESSION['user_id'] . "
                       )";

    $result = mysql_query($sql);



    {
        //the first query worked, now start the second, posts query
        //retrieve the id of the freshly created topic for usage in the posts query
        $topicid = mysql_insert_id();

        $sql = "INSERT INTO forumtopic
                    (
                       formtopic_forumcategory_id,
                       forumtopic_title,
                       forumtopic_text,
                       forumtopic_user_id,
                       forumtopic_date
                          )
                VALUES
                    ('" . mysql_real_escape_string($_POST['post_content']) . "',
                          NOW(),
                          " . $topicid . ",
                          " . $_SESSION['user_id'] . "
                    )";
        $result = mysql_query($sql);

        if(!$result)
        {
            //something went wrong, display the error
            echo 'An error occured while inserting your post. Please try again later.' . mysql_error();
            $sql = "ROLLBACK;";
            $result = mysql_query($sql);
        }
        else
        {
            $sql = "COMMIT;";
            $result = mysql_query($sql);

            //after a lot of work, the query succeeded!
            echo 'You have successfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';
        }
    }
}




 ?>



     ?>

最佳答案

调试 $result 变量

你的问题就在这里

    $result = mysql_query($query);

    if(!$result)

尝试添加

   $result = mysql_query($query);
   var_dump($result);
   exit();
   if(!$result)

并检查查询结果

因为你的问题是查询返回什么

关于php - 向论坛添加新主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23564040/

相关文章:

php - 按日期和时间排序并有限制地获取所有记录

javascript - PHP 将函数作为函数的参数执行

php - Phalcon,生成图像的 HTTP 缓存

mysql - 如何获取mysql数据库的大小?

php - 通过 TCP 将实时数据发送到网页

php - 编写连接 4 个表的 mysql 查询时遇到问题

php - MYSQL转Google图表(折线图)

php - 如何使用ajax自动更新数据库而不刷新?

mysql - 日志轮换 MySQL Windows 批处理

mysql - SQL Select 语句重复