...

View Full Version : Comments addon.



Idfk
07-02-2010, 08:07 PM
Hi,

Allday ive been trying to figure out how to add comments to my profiles pages but its not work so can anyone help me with this mysql and php code?


CREATE TABLE IF NOT EXISTS `comments` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL DEFAULT 'Guest',
`comment` text NOT NULL,
`id` int(11) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ;



<?php
$comment = mysql_query("SELECT FROM `comments` WHERE `id` = '$id'");
while($r = mysql_fetch_array($comment)) {
echo("$r[username]<br>$r[comment]");
}
?>
<?php
if($in[username]){
echo("
<form method=\"post\" name=\"comment\">
Username:<br />
<input type=\"text\" class=\"input\" name=\"username\" size=\"30\" maxlength=\"90\" value=\"$in[username]\"><br />
Message:<br />
<textarea cols=\"80\" class=\"input\" rows=\"5\" name=\"comment\"></textarea><br />
<input name=\"\" type=\"submit\" value=\"Add Comment\" />
</form>
");
}else{
echo "Please <a href=\"/login.php\">login</a> to leave a comment.";
}
if($_POST['comment']) {
mysql_query("INSERT INTO comments (userid,id,username,comment ) VALUES ('$in[id]','id','$in[username]','$r[comment]');");
echo("Comment added");
}

?>


Thanks.

tomws
07-02-2010, 08:15 PM
but its not work

A useful problem description would help.

Keleth
07-02-2010, 08:17 PM
I don't follow the pages logic.

At the top, you select a particular comment into $r. A little later, you allow someone to add a comment. Then at the bottom, if a comment was submitted, you try to insert a comment with variables that aren't defined anywhere or are defined by the selection of a particular comment. Either you're defining variables somewhere and not showing us, or you really need to reconsider how you're doing this.

Plus, in your insert, into `id`, an into, you're inserting the word 'id' which won't work (whether it'll set it to 0 or return a failure, I don't know).

Idfk
07-02-2010, 08:30 PM
A useful problem description would help.

I meant not working.

Annoyed with it big time lol.

Keleth
07-02-2010, 08:43 PM
Out of curiosity... why is "userid" your primary key in your comments table?

tomws
07-02-2010, 09:06 PM
I meant not working.


Yes, I gathered as much. That's why I said that a useful problem description would help. Here's a hint. What do you expect it to do? What is it actually doing? What errors/warnings/notices are you receiving?

Your post essentially demands, "Fix my crap." Not very appealing, you know?

Idfk
07-02-2010, 09:11 PM
Ive changed a fair bit on it and its added the comment to the mysql database

id,userid,username,comment
id= id of comment etc.
userid= the user's profile page its been added to.
username= the username that posted the comment.
comment= the comment the user post

That all works fine in the mysql database.

Now im having trouble displaying them on the user profile page to there id, its not displying anything. accept the from to add comments.

This is the code i have.


<?php
$comment = mysql_query("SELECT FROM `comments` WHERE `id` = '$id'");
$userid = mysql_query("SELECT FROM `comments` WHERE `userid` = '$userid'");
{
echo("
$username<br>
$comment
");
}
?>


Some how i think thats wrong but i dont know, seen as im new to php.

Keleth
07-02-2010, 09:57 PM
<?php
$comment = mysql_query("SELECT FROM `comments` WHERE `id` = '$id'");
$userid = mysql_query("SELECT FROM `comments` WHERE `userid` = '$userid'");
{
echo("
$username<br>
$comment
");
}
?>


Some how i think thats wrong but i dont know, seen as im new to php.

Yah, that's not really doing much of anything...

To extract data from a sql query, first, where are you defining $id? Second, all you've done in your code is to get a query response... you can't do much with it as you're trying. Specially when you haven't told SQL what columns you want back... either list them or a * for all.


$query = mysql_query("SELECT comment FROM `comments` WHERE `id` = $id");
list($comment) = mysql_fetch_array($query);


Fetch array actually lets you use the data within the query response. Also is a good idea to validate the result, which I haven't done above.

Second, what good is
$userid = mysql_query("SELECT FROM `comments` WHERE `userid` = '$userid'");

You're trying to get back the userid after you're searching for the userid with a particular userid? That's just a circular logic fail.

Then finally in your echo, you're echoing $username but don't show us where its defined. I assume that $userid = ... above this was supposed to be $username, but in that case, we're looking at a database which could use normalization so you don't get strange results, overpopulation, etc.

Idfk
07-02-2010, 10:14 PM
Well ive changed it to this


<?php
$query = mysql_query("SELECT * comment FROM `comments` WHERE `id` = '$a'");
list($comment) = mysql_fetch_array($query);
echo "$a[comment]";
?>

and i get this on outcome:


Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\profile.php on line 76


Doing my headin lmao!

Keleth
07-02-2010, 11:18 PM
Well... as for why the query doesn't work... I can tell you don't have any mysql knowledge either: "* comment" means nothing to the database. Either * for all, or comment for the comment column. If you want to get everything back, drop list ($comment) and change it a variable name of your choice.

And what is $a set to?

As for the echo... for the life of me, can't understand why you have $a[comment]... why would you think $a would have the comment value assigned to it? I'm probably coming off as a jerk right now, but i mean, this is elementary stuff, simple logic.

Idfk
07-03-2010, 11:38 AM
Anyone got a working select from database command with display because im not that a good coder and its doing my headin!

met
07-03-2010, 12:09 PM
this isnt a valid query



$query = mysql_query("SELECT * comment FROM `comments` WHERE `id` = '$a'");


just use




$query = mysql_query("SELECT * FROM `comments` WHERE `id` = $a");


and where are you getting $a from?


in essence you want something like this




$id=$_GET['id']; // from the URL

$qry=mysql_query("SELECT * FROM comments WHERE `id`= $id");

while($r=mysql_fetch_array($qry)) {
echo $r['author']; // etc for each field you want to display.
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum