简体   繁体   English

将带有表单的PHP变量发送到同一页面,但出现错误

[英]Sending a variable in PHP with a form to the same page, but I am getting error

Please Help, I am coding a page with PHP where users choose a subject, click on the link and are directed to a page where they can comment on.The user make use of a small form to comment on a subject. 请帮忙,我正在用PHP编写一个页面,用户可以在其中选择一个主题,单击链接,然后转到可以对其发表评论的页面。用户使用一个小表格对一个主题进行评论。 I am trying to submit the form to the same page. 我正在尝试将表单提交到同一页面。 So that the comment that the user submitted will display immediatly. 这样用户提交的评论将立即显示。 I am sending a variable with the form using the GET method, but I am getting: Database query failed: You have an error in your SQL syntax; 我正在使用GET方法发送带有表单的变量,但是我得到:数据库查询失败:SQL语法错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1, 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的''附近使用

but it is writing to my database, just not displaying the data 但它正在写入我的数据库,只是不显示数据

Here is my code(before head): 这是我的代码(在头之前):

<?php 

// GET subject_id from previous page and see if it has been set
if (isset($_GET['subj'])) {  
subject_id = $_GET['subj'];
} else {
$subject_id = NULL;
}


// process form that was submitted to same page
if (isset($_POST['submit'])) {

$subject_id = mysql_prep($_GET['subj']);
$commentbox = trim(mysql_prep($_POST['commentbox']));

$query = "INSERT INTO comments (
subject_id, content
) VALUES (
{$subject_id}, '{$commentbox}'
)";


if ($result = mysql_query($query, $connection)) {
// as is, $message will still be discarded on the redirect
$message = "The page was successfully created.";
redirect_to("blog_subject.php");
} else {
$message = "The page was unsuccessfully created.";
redirect_to("blog_subject.php");
}
}
?>

......and then the code to display the subject,comments and form... ......然后是显示主题,评论和表格的代码...

<?php
// Retrieving Subject Name from subjects database
global $connection;
if (isset($_GET['subj'])) {$subject_id = $_GET['subj'];} 

$query = "SELECT * FROM subjects WHERE subject_id = {$subject_id}";

$result_set = mysql_query($query, $connection);

if (!$result_set) {die("Database query Failed: " . mysql_error());}

if ($subject_set = mysql_fetch_array($result_set)) {

echo $subject_set['subject_name'];

} else {

return NULL;

}
?>
<br />
<?php
// Retrieving Comments from comments database

global $connection;

$subject_id = $_GET['subj'];

$comment_set = mysql_query("SELECT * FROM comments WHERE subject_id =
{$subject_id}", $connection);

if (!$comment_set) {
die("Database query Failed: " . mysql_error());
}

echo "<ul class=\"pages\">";
while ($comment_for_subject = mysql_fetch_array($comment_set)) {
echo "<li>{$comment_for_subject["content"]}</li>";
}
echo "</ul>";   
?>
<br /> 

<form action="blog_subject.php?subj=<?php echo $subject_id_van_vorige_bladsy; ?>"
method="post">
<textarea name="commentbox" cols="100" rows="10"></textarea><br />
<input name="submit" type="submit">
</form>

Is subject_id a string or a number? subject_id是字符串还是数字? if it is a string you need to escape it like this: 如果是字符串,则需要像这样转义:

$comment_set = mysql_query("SELECT * FROM comments WHERE subject_id = '{$subject_id}'", $connection);

If that is not it, please do an echo on $comment_set and post the output of that 如果不是,请在$ comment_set上执行echo并发布该输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM