Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: Comments addon.

  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Location
    Sutton-in-ashfield, Notts
    Posts
    117
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Comments addon.

    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 Code:
    <?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.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by Idfk View Post
    but its not work
    A useful problem description would help.
    Are you a Help Vampire?

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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).

  • #4
    Regular Coder
    Join Date
    Jun 2010
    Location
    Sutton-in-ashfield, Notts
    Posts
    117
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tomws View Post
    A useful problem description would help.
    I meant not working.

    Annoyed with it big time lol.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Out of curiosity... why is "userid" your primary key in your comments table?

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by Idfk View Post
    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?
    Are you a Help Vampire?

  • #7
    Regular Coder
    Join Date
    Jun 2010
    Location
    Sutton-in-ashfield, Notts
    Posts
    117
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by Idfk View Post
    PHP Code:
    <?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.
    PHP Code:
    $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
    PHP Code:
    $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.

  • #9
    Regular Coder
    Join Date
    Jun 2010
    Location
    Sutton-in-ashfield, Notts
    Posts
    117
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Well ive changed it to this
    PHP Code:
    <?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!

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,537
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • #11
    Regular Coder
    Join Date
    Jun 2010
    Location
    Sutton-in-ashfield, Notts
    Posts
    117
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Anyone got a working select from database command with display because im not that a good coder and its doing my headin!

  • #12
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    this isnt a valid query

    PHP Code:
    $query mysql_query("SELECT * comment FROM `comments` WHERE `id` = '$a'"); 
    just use

    PHP Code:

    $query 
    mysql_query("SELECT * FROM `comments` WHERE `id` = $a"); 
    and where are you getting $a from?


    in essence you want something like this

    PHP Code:

    $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.

    Last edited by met; 07-03-2010 at 12:12 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •