我正在尝试创建仅允许登录用户在论坛中创建新主题的代码。
我有一个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/