Page 1 of 1

View comments on profile page need help.

Posted: Sun Apr 15, 2012 8:17 pm
by Smg
ok recently i have been trying to make a simple view comments kind of like facebook like say...
when you post a comment on the users profile under the comment will be a link to see all the comments posted onto that one comment.
things i got to work with this...
- viewing the comment (when clicking view comments brings you to that page)

things that is not working...
- posting the comment onto the new page with the comments from the single comment on the profile page.
- also when i enter the comment it does add it to the database but its not getting the id...

ok so i need help trying to figure out how to get this to work here is what i have so far.

SQL:
user comments:
"also im using the same sql for adding comments on the profile page"
[syntax=sql]CREATE TABLE IF NOT EXISTS `user_comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`text` varchar(50) NOT NULL,
`comment` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
[/syntax]

the SQL that doesnt add:
[syntax=sql]INSERT INTO `user_comments` (`id`, `user_id`, `text`, `comment`) VALUES
(1, 0, 'Smg', 'Enter your comment');
[/syntax]

view_comment.php:
[syntax=php]<?php
include('core/init.inc.php');

$id = $_GET['id'];
if(empty($id))
header('Location: index.php');
else {
$query = mysql_query("SELECT * FROM user_comments WHERE id = '$id' ORDER BY id DESC");
if (mysql_num_rows($query) == 0)
echo 'Be the first to add a comment.<br />';
else
{
while($output = mysql_fetch_assoc($query))
{
echo $output['text'].'<br />';
echo $output['comment'].'<br />';
}
} ?>
<form method="post" action="user_comment.php?id=<?php echo $id; ?>">
<input type="hidden" name="name" id="name" value="Name" /><br />
<textarea name="comment" id="comment">Enter your comment</textarea><br />
<input type="submit" name="submit" id="submit" value="Submit" />
<input type="hidden" value="'.$id.'" name="id" id="id" />
</form>
<?php } ?>[/syntax]

user_comment.php:
[syntax=php]<?php
include('core/init.inc.php');

$id = $_GET['id'];
if(empty($id))
header('Location: index.php');
else
{
function clear($message)
{
if(!get_magic_quotes_gpc())
$message = addslashes($message);
$message = strip_tags($message);
$message = htmlentities($message);
return trim($message);
}
if($_POST['submit'])
{
if (empty($_POST['comment']))
die('Enter a comment.');
$id = $_POST['id'];
$msg_title = $_SESSION['user_username'];
$msg_message = clear($_POST['comment']);
if(mysql_query("INSERT INTO `user_comments` VALUES ('', '', '{$msg_title}', '{$msg_message}')"))
echo 'Comment Entered.';
mysql_close();
}
}
?>[/syntax]
[syntax=php][/syntax]

Re: View comments on profile page need help.

Posted: Tue Apr 17, 2012 6:32 pm
by jacek
I would set this up with two tables, one for profile_comments and another for comment_replies. Then you can just have two forms one on the profile page that adds to the profile_comments table and another under each comment that adds to the comment_replies table. then it should just be a matter of getting the data from the appropriate table. Make sense ?

You should really try to keep you SQL out of the actual pages too, so instead of doing

[syntax=php] $query = mysql_query("SELECT * FROM user_comments WHERE id = '$id' ORDER BY id DESC");
if (mysql_num_rows($query) == 0)
echo 'Be the first to add a comment.<br />';
else
{
while($output = mysql_fetch_assoc($query))
{
echo $output['text'].'<br />';
echo $output['comment'].'<br />';
}
}[/syntax]
you should have a function

[syntax=php] if (($comments = fetch_profile_comments($id)) == false)
{
echo 'Be the first to add a comment.<br />';
}
else
{
foreach ($comments as $comment)
{
echo $comment['text'].'<br />';
echo $comment['comment'].'<br />';
}
}[/syntax]
It will make everything a lot easier to work with later on.

Function should also be kept in separate files for the same reason.

Re: View comments on profile page need help.

Posted: Tue Apr 17, 2012 10:45 pm
by Smg
ok ill do a function later but atm i want to get this to work so... so far i got the comment_replies to work in the sql database but its not grabbing the comment_id and pasting it into the database so what should i do for this to work?

SQL:
[syntax=sql]
id int(11) No None AUTO_INCREMENT
comment_id int(11) No None
name varchar(25) latin1_swedish_ci No None
reply varchar(100) latin1_swedish_ci No None[/syntax]

user_comment.php:
[syntax=php]<?php
include('core/init.inc.php');

$id = $_GET['id'];
if(empty($id))
header('Location: index.php');
else
{
function clear($message)
{
if(!get_magic_quotes_gpc())
$message = addslashes($message);
$message = strip_tags($message);
$message = htmlentities($message);
return trim($message);
}
if($_POST['submit'])
{
if (empty($_POST['reply']))
die('Enter a comment.');
$id = mysql_query("SELECT * FROM `comment_replies` WHERE `comment_id` = {$_POST['comment_id']} ORDER BY id DESC");
$comment_id = $_GET['id'];
$msg_title = $_SESSION['user_username'];
$msg_message = clear($_POST['reply']);
if(mysql_query("INSERT INTO `comment_replies` VALUES ('', '{$comment_id}', '{$msg_title}', '{$msg_message}')"))
echo 'Comment Entered.';
mysql_close();
}
$ref = $_SERVER['HTTP_REFERER'];
header('refresh: 0.5; url='.$ref);
}
?>[/syntax]

view_comment.php:
[syntax=php] <!-- TemplateBeginEditable name="content" -->
<center>
<div align="center" class="content" id="content"><br />
<?php
$id = $_GET['id'];
if(empty($id))
header('Location: index.php');
else {
$query = mysql_query("SELECT * FROM comment_replies WHERE id = '$id' ORDER BY id DESC");
if (mysql_num_rows($query) == 0)
echo 'Be the first to add a comment.<br />';
else
{
?>
<table width="350" align="center" border="0">
<?php
while($output = mysql_fetch_assoc($query))
{
?>
<tr>
<td><?php echo $output['name'].'<br />'; ?></td>
<td><?php echo $output['reply'].'<br />'; ?></td>
</tr>
<?php
}
}

$_POST['id'] = $_GET['comment_id'];
?>
</table>
<form method="post" action="user_comment.php?id=<?php echo $id; ?>">
<input type="hidden" name="name" id="name" value="Name" /><br />
<textarea name="reply" id="reply">Enter your comment</textarea><br />
<input type="submit" name="submit" id="submit" value="Submit" />
<input type="hidden" value="'.$id.'" name="id" id="id" />
</form>
</div></center><br /><!-- TemplateEndEditable -->[/syntax]

Re: View comments on profile page need help.

Posted: Thu Apr 19, 2012 3:13 pm
by jacek
I don't see where $comment_id is defined, that could be it ?

Re: View comments on profile page need help.

Posted: Fri Apr 20, 2012 1:15 am
by Smg
ok i got it to get the id into the database now it is not showing the comment.... so what do i do now?
i also updated the user_comment.php above where you said there was no $comment_id

also to show i got the id to work into comment_id:
[syntax=sql]
id: comment_id: name: reply:
1 23 Smg Enter your comment
2 21 Smg OMFG
3 21 Smg Enter your comment[/syntax]

Re: View comments on profile page need help.

Posted: Sun Apr 22, 2012 8:24 pm
by jacek
Smg wrote:ok i got it to get the id into the database now it is not showing the comment....

Can you post the code that should show the comments ?

It is probably somethign simple, are you sure you are running the correct query. It could be that you are not fetching the comments for the right ID.

Re: View comments on profile page need help.

Posted: Mon Apr 23, 2012 10:05 pm
by Smg
Ok here is the code and I checked I can't figure it out

user_comment.php:
[syntax=php]<?php
include('core/init.inc.php');

$id = $_GET['id'];
if(empty($id))
header('Location: index.php');
else
{
function clear($message)
{
if(!get_magic_quotes_gpc())
$message = addslashes($message);
$message = strip_tags($message);
$message = htmlentities($message);
return trim($message);
}
if($_POST['submit'])
{
if (empty($_POST['reply']))
die('Enter a comment.');
$id = mysql_query("SELECT * FROM `comment_replies` WHERE `comment_id` = {$_POST['comment_id']} ORDER BY id DESC");
$comment_id = $_GET['id'];
$msg_title = $_SESSION['user_username'];
$msg_message = clear($_POST['reply']);
if(mysql_query("INSERT INTO `comment_replies` VALUES ('', '{$comment_id}', '{$msg_title}', '{$msg_message}')"))
echo 'Comment Entered.';
mysql_close();
}
$ref = $_SERVER['HTTP_REFERER'];
header('refresh: 0.5; url='.$ref);
}
?>[/syntax]

view_comment.php:
[syntax=php] <!-- TemplateBeginEditable name="content" -->
<center>
<div align="center" class="content" id="content"><br />
<?php
$id = $_GET['id'];
if(empty($id))
header('Location: index.php');
else {
$query = mysql_query("SELECT * FROM comment_replies WHERE id = '$id' ORDER BY id DESC");
if (mysql_num_rows($query) == 0)
echo 'Be the first to add a comment.<br />';
else
{
?>
<table width="350" align="center" border="0">
<?php
while($output = mysql_fetch_assoc($query))
{
?>
<tr>
<td><?php echo $output['name'].'<br />'; ?></td>
<td><?php echo $output['reply'].'<br />'; ?></td>
</tr>
<?php
}
}

$_POST['id'] = $_GET['comment_id'];
?>
</table>
<form method="post" action="user_comment.php?id=<?php echo $id; ?>">
<input type="hidden" name="name" id="name" value="Name" /><br />
<textarea name="reply" id="reply">Enter your comment</textarea><br />
<input type="submit" name="submit" id="submit" value="Submit" />
<input type="hidden" value="'.$id.'" name="id" id="id" />
</form>
</div></center><br /><!-- TemplateEndEditable -->[/syntax]

Re: View comments on profile page need help.

Posted: Tue Apr 24, 2012 9:35 pm
by jacek
Should one of those pages be using a different table ? They are both using comment_replies at the moment.

Re: View comments on profile page need help.

Posted: Tue Apr 24, 2012 9:40 pm
by Smg
Not sure but it works I just need it to display the comments from comment replies

Re: View comments on profile page need help.

Posted: Thu Apr 26, 2012 12:28 pm
by jacek
Smg wrote:Not sure but it works I just need it to display the comments from comment replies

Okay, one of them should be the other table then. Which ever page should show the replies needs to get the data from the comments replies table.

Re: View comments on profile page need help.

Posted: Thu Apr 26, 2012 8:58 pm
by Smg
i got it fixed it was with a part of the code that i just had to remove then it started working thank you for the help :D

Re: View comments on profile page need help.

Posted: Sat May 05, 2012 11:14 pm
by sevvlor
One more thing, I cannot find the mysql_real_escape(). Which is needed to prevent MySQL injections.

You use it on any user defined value, I mean post, get and cookies.