Wednesday, 11 September 2013

php variable gets null at the end of the script?

php variable gets null at the end of the script?

I have a problem that I really can't figure out. basically I'm working on
a blog and I have a variable from a cookie that returns a post id. the
problem is that at the end of the script, when I enter an if(isset)
statement, the variable seems to be null and I can't understand the reason
behind it. it won't add anything to the database and it won't redirect to
the post id. here's the script:
<?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>
as you can see, I'm echoing that variable in the textarea1 just before the
if statement and it returns the correct value, but at the end it's null.
thanks in advance.

No comments:

Post a Comment