我有一个问题,我真的想不通。基本上我正在写一个博客,我有一个来自 cookie 的变量,它返回一个帖子 ID。问题是在脚本末尾,当我输入 if(isset) 语句时,变量似乎为空,我无法理解其背后的原因。它不会向数据库添加任何内容,也不会重定向到帖子 ID。这是脚本:
<?php
//display the post
$post_id = $_GET['post_id'];
$query = mysql_query("SELECT * FROM posts WHERE `post_id`='". $post_id. "';") or die(mysql_error());
if(mysql_num_rows($query) != 0)
{
$currentrow = mysql_fetch_array($query);
echo $currentrow['title']. "<br>". $currentrow['text'];
}
else
{
echo "that post does not exist.";
}
?>
</div>
<div id="comments">
<br>
<h3>Comments</h3>
<br>
<?php
//display the comments
$comments = mysql_query("SELECT * FROM `comments` JOIN `posts` ON(posts.post_id=comments.post_id) JOIN `users` ON(users.user_id=comments.user_id) WHERE posts.post_id=". "'". $_GET['post_id']. "'". ";")or die(mysql_error());;
while($currentRow = mysql_fetch_array($comments))
{
echo $currentRow['ctext']. "<br>";
echo "posted by <a href='/templates/profile.php?name=". $currentRow['name']. "'>". $currentRow['name']. "</a> at ". $currentRow['cdate']. " -- ". $currentRow['ctime']. "<br><br>";
}
?>
</div>
<Form Name ="submitform" Method ="POST" ACTION = "post.php">
<div id="commentbox">
<textarea name="textarea1" rows="10" cols="30">
<?php
echo $post_id;
?>
</textarea>
<br>
<input type="submit" name="submitcomment" value="Submit"><br>
</div>
</Form>
<?php
if(isset($_POST['submitcomment']))
{
$comment = $_POST['textarea1'];
$user_id = $_COOKIE['userid'];
mysql_query("INSERT INTO comments(`ctext`, `cdate`, `ctime`, `user_id`, `post_id`) VALUES('$comment', 'CURDATE()', 'CURTIME()', '$user_id', '$post_id')") or die(mysql_error());
header('Location: /post.php?post_id='. $post_id);
}
?>
</body>
</html>
如您所见,我在 if 语句之前的 textarea1 中回显该变量,它返回正确的值,但最后它为空。提前致谢。