Comments System

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thoughtriot
Forum Commoner
Posts: 26
Joined: Thu Nov 07, 2002 9:32 pm

Comments System

Post by thoughtriot »

Okay, I'm making a commenting system on my news blog type thing. I have a table called comments.

In the comments table, I have postid (that's the field that keeps the ID of the news entry so I can just call the comments for that news entry), and on my comments.php page, where you submit new comments, I have it so it's comments.php?postid=newsid

That all works, but once I submit one comment for an entry, it doesn't work anymore because I suppose if I have let's say 82 in the postid field in the comments table, I can't have 82 in that field again, just like for ID, you can't have 2 entries with the ID of 1 or something. So what should I do so there can be more than 1 comment with the postid of 82 for instance?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

give each comment its own unique ID and have that auto_increment. then as the next field have you postid field with no PRIMARY or UNIQUE indexes
thoughtriot
Forum Commoner
Posts: 26
Joined: Thu Nov 07, 2002 9:32 pm

Post by thoughtriot »

I'm not exactly sure how to do that for the postid. I do have a field called ID which is auto_increment and is a unique field. For postid I have it as
int(10) NOT NULL default 0

And here's my PHP code if that might be the problem:

Code: Select all

<?
include('news/connect.php');
$sql = "SELECT * FROM comments WHERE postid='$postid' ORDER BY id DESC";
$query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
while($result = mysql_fetch_array($query)) {
$Email = stripslashes($result["Email"]);
$Date = stripslashes($result["Date"]);
$Name = stripslashes($result["Name"]);
$Entry = stripslashes($result["Entry"]);
$ID = $result["ID"];

echo "<table width=217 cellpadding=0 cellspacing=0 class=blanktable><tr class=blanktable><td><a href=mailto:$Email>$Name</a> on <span class=dates>$Date</span><br>" . nl2br($Entry) . "</td></tr><tr class=blanktable><td height=36 background='images/headlinebottom.gif' class=blanktable></td></tr></table><br>";
}


if($submit)
{
  $result=MYSQL_QUERY("INSERT INTO comments (ID,Name,Email,Date,Entry,postid)".
  "VALUES ('NULL', '$Name', '$Email', '$Date', '$Entry', '$postid')");
  print "Comments have been added";
}
else
{
?>
<form method="post" action="comments.php">
<span class="header2">Add Comments</span><br>
Name:<br>
<INPUT TYPE='TEXT' NAME='Name' class=input size=27><br>
<img src="images/form.gif" border=0><br>
Email:<br>
<INPUT TYPE='TEXT' NAME='Email' class=input size=27><br>
<img src="images/form.gif" border=0><br>
Comments:<br>
<textarea rows=8 cols=27 NAME='Entry' class=inputbox></textarea><br>
<INPUT TYPE="submit" name="submit" value="Submit" class=button>
<input type="hidden" name="postid" value="<? echo $postid; ?>">
<input type="hidden" name="Date" value="<? echo "" . date("m.d.y", time() -10800) . ""; ?>">
</form>
<?
    }
?>
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

do you have a working example of this? the code looks fine to me but it may help if i saw what was actually happening.
thoughtriot
Forum Commoner
Posts: 26
Joined: Thu Nov 07, 2002 9:32 pm

Post by thoughtriot »

I fixed it. It's all working now
Post Reply