我正在尝试编写“创建主题”页面的代码。用户将添加一个带有文本的主题,每个主题都有一个向下滚动选项和一个文本框。接口已编码。我正在尝试做的是将数据保存在数据库中。我被困在我的代码不断向我推送错误的某个时刻,我认为这是因为查询后出现“值”。这是我的代码:
我尝试了不同的“值”语法,因为我认为这是问题的根源,但我不确定,因此也来这里。
这是工作界面,其中包含每个输入数据的名称:
<?php
include ("server.php");
echo '<h2>Create a topic</h2>';
//the user is signed in
if($_SERVER['REQUEST_METHOD'] != 'POST')
//the form hasn't been posted yet, display it
//retrieve the categories from the database for use in the dropdown
$sqli = "SELECT cat_id, cat_name, cat_description FROM categories";
$result = mysqli_query($db,$sqli);
if(!$result)
{
//the query failed, uh-oh :-(
echo 'Error while selecting from database. Please try again later.';
else
{
echo '<form method="post" action="">
Subject: <input type="text" name="topic_subject" />
Category:';
echo '<select name="topic_cat">';
while($row = mysqli_fetch_assoc($result))
{
echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
}
echo '</select>';
echo 'Message: <textarea name="post_content" /></textarea>
<input type="submit" value="Create topic" />
</form>';
}
}
好的,这部分代码可以正常工作。当用户按下“创建主题”时会发生此问题
这是其余的代码:
else
{
//start the transaction
$query = "BEGIN WORK;";
$result = mysqli_query($db,$query);
if(!$result)
{
//Damn! the query failed, quit
echo 'An error occured while creating your topic. Please try again later.';
}
else
{
//the form has been posted, so save it
//insert the topic into the topics table first, then we'll save the post into the posts table
$sqli = "INSERT INTO topics (topic_subject, topic_cat, topic_by)
VALUES('" . mysqli_real_escape_string($_POST['topic_subject']) . ",
" . mysqli_real_escape_string($_POST['topic_cat']) . ",
" . $_SESSION['username'] . "
')";
$result = mysqli_query($db,$sqli);
if(!$result)
{
//something went wrong, display the error
echo 'An error occured while inserting your data. Please try again later.' . mysqli_error();
$sqli = "ROLLBACK;";
$result = mysqli_query($db,$sqli);
}
else
...
我得到的错误是:插入您的数据时发生错误。请稍后再试”,因此问题必须出在代码的这一部分,但找不到位置。可能是来自“。” $ _SESSION ['username']“?应该存储在server.php文件的会话中。.
谢谢大家的帮助 祝你有美好的一天