Page 1 of 1

Comments script - logged in username not appearing

Posted: Fri Apr 17, 2009 12:07 pm
by anivad
This is driving me crazy. I'm trying to have a vistor's username displayed when they are logged in, and have this field replaced with 'Guest' if they are not. But the code keeps echoing 'Guest' no matter what.

($scheck is defined earlier - it's basically to determine logged in status, and that code works elsewhere.)

Code: Select all

<? 
include 'commentstest.php';
$uname = $_SESSION['uname'];
?>
 
<p><center>
<b>Post Comment</b>
<form name="comments" method="post" action="commentstest.php">
<input type="hidden" name="page" value="<? print($_SERVER['REQUEST_URI']) ?>">
<input type="hidden" name="date" value="<? print date(); ?>"> 
 
<table border="0">
<tr>
<td class="c"></td><td class="c"><b><? 
if(!$scheck) {
$cname = "Guest";
print $cname;
}
else {
$cname = $uname;
print $cname;
}
?>
</b></td></tr>
<tr><td class="c" align="right"><b>Subject</b></td><td class="c"><input type="text" size="50" name="subject"></td></tr>
<tr><td class="c" align="right"><b>Comment</b></td><td class="c"><textarea name="comment" cols="45" rows="5" wrap="VIRTUAL"></textarea></td></tr>
<tr><td class="c" align="right" colspan="2"><input type="submit" name="submit" value="Add Comment"></td></tr>
</table>
</form></center>
commentstest.php:

Code: Select all

<? 
 
include '../db.php';
 
$sql = "SELECT * FROM comments WHERE page= '".stripslashes($_SERVER['REQUEST_URI'])."'";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if ($result) {
    if ($num_rows > 0) {
    
$inf = "SELECT * FROM comments WHERE page= '".stripslashes($_SERVER['REQUEST_URI'])."' ORDER BY postdate ASC"; 
$info = mysql_query($inf);
if(!$info) die(mysql_error());
 
if($info) {
$data = mysql_fetch_assoc($info);
 
print "<b>Comments</b>";
print "<p><table border='1' bordercolor='black'>";
 
while($info2 = mysql_fetch_object($info)) {
print "<tr><td class='sm' colspan='2' width='800'>" .stripslashes($info2->subject);
print "</td></tr><tr><td class='c' width='150'>" .stripslashes($info2->cname);
print "<br>" .stripslashes($info2->postdate). "</td>";
print "<td valign='top' class='c'>" .stripslashes($info2->comment). "</td></tr>";
}//end while
print "</table>";
print "<hr>";
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
 
$comment = $_POST['comment'];
 
if ($comment == '') { 
       error('Comment field left empty. Please fill it in and try again.'); 
}
else {
 
$comment = mysql_real_escape_string($comment);
$subject = $_POST['subject'];
$subject = mysql_real_escape_string($subject);
$page = $_POST['page'];
$date = $_POST['date'];
 
$sql = "INSERT INTO comments SET page='$page', uname='$cname', postdate='$date', subject='$subject', comment='$comment'";
$result = mysql_query($sql);
 
if (!result) {
error('A database error occured during submission');
}
else {
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_POST['page'] . "#comments"); 
}
}
}
}
?>
And for some reason, the page only displays 2 comments and stops there.

One last thing - I can't get the date to display. I set the MySQL table column to DATETIME, and all I get is 0000-00-00 00:00:00 for every entry.

I started trying to define date("F d, y, H:i") but had the same problem, so I gave up and left it as date() and I'm still getting the string of zeroes.

Help on all matters (especially the first two) would be appreciated. Thanks!

EDIT: And now for some reason the entries aren't even getting inserted into the database. Weird. It worked earlier.

Re: Comments script - logged in username not appearing

Posted: Fri Apr 17, 2009 1:08 pm
by califdon
You are setting $cname = $uname (if there is a value for $scheck), and $uname = a session variable. But you haven't showed us any code for setting the session variable(s). But since you say that it's always showing "Guest", the answer is that $scheck is set to False, so that's your problem. It's in the code that you haven't shown to us.

As for the "only 2 rows" question, it would appear that it's only finding 2 rows that meet your WHERE criteria. Do you have any reason to believe that there should be more than 2 rows? I would certainly examine the data in your table in a test case to see what's actually there.

The format you need for a DateTime data type would be: date('Y-m-d H:i:s'), to match the "0000-00-00 00:00:00" that you see when it defaults.

Re: Comments script - logged in username not appearing

Posted: Fri Apr 17, 2009 10:24 pm
by anivad
Yeah, turned out that $scheck was set to false. :? That's what happens when I try to do anything when I'm half asleep. Fixed it and it works now. Thanks!

I think there was some other problem with the two rows thing, because after fixing the rest of the code that suddenly started working again.

Still not printing $cname, but that might be some minor syntax wrong somewhere.