SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question user comment form

    Hi

    I want to add a user comment field on every article that I have.
    I have three tables for that. One with the article text, one with articleid and commentid, and one with the comment text.

    I need the date to be date with timestamp as well, so I can make a flood protection function later.

    I don't know how to make this many-to-many user comment form. I've started on something, but I get more frustrated every minute since I can't figure out how to update the many-to-many relation and query.

    Can anyone please help me?

    Here are the tables:
    commenttable: id,name,mail,comment,datetime
    articlecommenttable: commentid, articleid
    articletable: id, text, date, heading


    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Add new comment</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <?php

    $dbcnx 
    = @mysql_connect("xxx""xxxx""Xxxx"); 
    if (!
    $dbcnx) {
      exit(
    '<p>Unable to connect to the ' .
          
    'database server at this time.</p>');
    }

    if (!@
    mysql_select_db('articledatabase')) {
      exit(
    '<p>Unable to locate the joke ' .
          
    'database at this time.</p>');
    }

    $currentarticleid $_GET['ID'];

    if (isset(
    $_POST['comment'])): 


     
    $name $_POST['name'];
     
    $mail $_POST['mail'];
     
    $comment $_POST['comment'];


      if (
    $mail == '') {
        exit(
    '<p>You must write your email address. Click "Back" and try again.</p>');
      }

    ?>




    <p><a href="comment.php">View comments</a></p>

    <?php
    else: 


      
    $coms = @mysql_query('SELECT id, name, mail, comment, date FROM comment');
      if (!
    $coms) {
        exit(
    '<p>Unable to obtain category list from the database.</p>');
      }
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>name:<br />
    <textarea name="name" rows="1" cols="30"></textarea></p>
    <p>Mail:<br />
    <textarea name="name" rows="1" cols="30"></textarea></p>
    <p>comment:<br />
    <textarea name="comment" rows="5" cols="45">
    </textarea></p>
    <input type="hidden" name="date" value="$date" />
    <input type="submit" value="SUBMIT" />
    </form>
    <?php endif; ?>
    </body>
    </html>

  2. #2
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see the need to have 3 tables in this case.
    In your comment table, have a field that references the article in the article table. It's a simple 1 to many relationship.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so now I changed to just two tables. But how do I use the _GET['id'] from the article.php?id=1 so that each article won't get all the comments from the other articles?
    Sorry for the bad English, it's a little hard to explain, hope someone understands what I mean.
    And how do I get the $date with date and time?

    PHP Code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Comments</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <?php
    if (isset($_POST['kommentar'])):

    $dbcnx = @mysql_connect("xxx""Xxx""Xxxx"); 
    if (!
    $dbcnx) {
      exit(
    '<p>Unable to connect to the ' .
          
    'database server at this time.</p>');
    }

    if (!@
    mysql_select_db('mydatabase)) {
      exit('
    <p>Unable to locate the joke ' .
          '
    database at this time.</p>');
    }

    $carticleid = $_GET['
    id'];



    $name = $_POST['
    name'];
    $mail = $_POST['
    mail'];
    $kommentar = $_POST['
    kommentar'];


      if ($mail == '') {
        exit('
    <p>You must write your email addressClick "Back" and try again.</p>');
      }

     $sql = "INSERT INTO kommentar SET
              navn='
    $name',
              mail='
    $mail',
              kommentar='
    $kommentar',
              dato='
    $date'";

      if (@mysql_query($sql)) {
        echo '
    <p>Comment inserted. <a href="comment.php">View comments</a></p>';
      } else {
        exit('
    <p>Error updating joke details' .
            mysql_error() . '
    </p>');

    }
    ?>

    <p><a href="comment.php">View comments</a></p>

    <?php else: ?>
    <form action="<?php echo $_SERVER['
    PHP_SELF']; ?>" method="post">
    <p>name:<br />
    <textarea name="name" rows="1" cols="30"></textarea></p>
    <p>Mail:<br />
    <textarea name="mail" rows="1" cols="30"></textarea></p>
    <p>comment:<br />
    <textarea name="kommentar" rows="5" cols="45">
    </textarea></p>
    <input type="hidden" name="date" value="$date" />
    <input type="submit" value="SUBMIT" />
    </form>
    <?php endif; ?>



    </body>
    </html>

  4. #4
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume you are asking about the SQL to do all this?

    What is the new format of your tables?

    I'll go with an assumption here.
    The SQL can be as follows (based on your script posted):
    PHP Code:
    $sql "INSERT INTO kommentar (articleid, navn, mail, kommentar, dato)
            VALUES (
    $carticleid,'$name', '$mail', '$kommentar', NOW())"
    Here I assume that the field that references the article is called <articleid>.
    You capture the article ID like so:
    PHP Code:
    $carticleid $_GET['id']; 
    So, use that value <$carticleid> in the SQL comman.
    NOW() is used to submit the current time for the server. I assume that's what you want.
    Your initial SQL query wasn't entirely correct. SET is used with UPDATE statements.

    Hope that helps.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help so far
    I fixed the query for displaying comments under articles.
    I can now add things to the kommentar table.
    The problem is that the $id won't work when someone clicks View comments. The link is still $id and not the number.
    Another problem for the flood protection is Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource.

    How do I fix this?

    PHP Code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Comments</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <?php
    $carticleid 
    $_GET['id'];
    if (isset(
    $_POST['kommentar'])):

    $dbcnx = @mysql_connect("xxx""Xxx""Xxxx"); 
    if (!
    $dbcnx) {
      exit(
    '<p>Unable to connect to the ' .
          
    'database server at this time.</p>');
    }

    if (!@
    mysql_select_db('database')) {
      exit(
    '<p>Unable to locate the ' .
          
    'database at this time.</p>');
    }


    $name $_POST['name'];
    $mail $_POST['mail'];
    $kommentar $_POST['kommentar'];


      if (
    $mail == '') {
        exit(
    '<p>You must write your email address. Click "Back" and try again.</p>');
      }

    $query "SELECT time FROM kommentar WHERE navn='$name' ORDER BY tid DESC LIMIT 1;";
    $tmp mysql_fetch_assoc(mysql_query($query)); 
    if (
    $tmp['time']>(time()-3600)) {
    echo 
    "Can't post, it's not more than an hour ago since $navn wrote something";
    }
    else { 
     
    $sql "INSERT INTO kommentar SET
              navn='
    $name',
              mail='
    $mail',
              kommentar='
    $kommentar',
              time='"
    .time()."',
              artikkelid='
    $carticleid'";

      if (@
    mysql_query($sql)) {
    $id $carticleid;
        echo (
    '<p>Comment inserted. <a href="view.php?id=$id">View comments</a></p>');
      } else {
        exit(
    '<p>Error updating joke details: ' .
            
    mysql_error() . '</p>');

    }}
    ?>


    <?php else: ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>name:<br />
    <textarea name="name" rows="1" cols="30"></textarea></p>
    <p>Mail:<br />
    <textarea name="mail" rows="1" cols="30"></textarea></p>
    <p>comment:<br />
    <textarea name="kommentar" rows="5" cols="45">
    </textarea></p>
    <input type="submit" value="SUBMIT" />
    </form>
    <?php endif; ?>



    </body>
    </html>

  6. #6
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the script you posted supposed to achieve?
    Right now, it's functionality is to add a comment into the kommentar table.
    To view the comments, all you need is a select statement and a loop to iterate through them.
    PHP Code:
    $sql "SELECT * FROM kommentar WHERE artikkelid = $id ORDER BY time";
    $result mysql_query($sql);
    while (
    $row mysql_fetch_assoc($result) {
        
    $navn $row['navn'];
        
    $mail $row['mail'];
        
    $kommentar $row['kommentar'];
        
    $time $row['time'];

        echo 
    "<div>";
        echo 
    "<p>" $kommentar "</p>";
        echo 
    "<p>Comment made by " $navn "(" $mail ") at " $time "</p>";
        echo 
    "</div>";
        } 

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I made a flood protection so that a user can't post a new comment for 1 hour. But it didn't work, I got an errormessage.
    And when a user click a href="view.php?id=$id" View comments</a>.
    A view page with all comments for the articleid is suppose to show up, but the $id is not returning the articleid number.
    This script is only for adding a comment to an article. And it is suppose to havea floodprotection.

  8. #8
    *munch munch* killerkooki's Avatar
    Join Date
    Sep 2004
    Location
    Singapore
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the script which has the link to view.php?
    And as for the flood protection, run the query by itself and see what error it gives. Might help you troubleshoot it.
    It's late here and I am off to bed. If you still need help when I get up later, I'll be sure to try and give it a shot.
    Good luck.


Bookmarks

Posting Permissions

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