• I’m trying to insert a comment into the database through writing my own comment form. Can anyone take a look at it and tell me what I should change to make it work? I get a 404 error when I click submit on a comment.

    $post_id = $post['post_id'];
                        echo '<form method="index.php" action="post" Comment <input type="text" name="comment"/>';
    
                             if ($user_ID) {
                                  echo '<input type="text" name="author" style="display:none" value="$user_login"/>';
                                  echo "logged in as $user_login";
                                  }
                             else echo 'Name <input type="text" name="author"/>';
                             echo '<input type="text" name="post_id" style="display: none;" value="'.$post_id.'"/>';
                             echo '<p><input name="submit" type="submit" id="submit" value="submit" tabindex="5" /></p>';
    
                             if (empty($_POST['comment_post_id']) || !(($post_id = (int) $_POST['comment_post_id']) > 0)) {
                                  # bad comment_post_id;
                                  }
                             $sql=("INSERT INTO wp_comments(comment_author, user_id, comment_content) VALUES ('$_POST[author]','$user_login', '$_POST[comment]')");
    
                        echo '</form>';
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter shatterjack

    (@shatterjack)

    i switched method and action accidentally but it still won’t insert the comment. If I use

    action=’.$_SERVER[‘PHP_SELF’].’

    it gives me a fatal error – Cannot use string offset as an array

    for this line: $author[$row[‘user_nicename’]][] = $row;
    which is from an earlier part of the code and works fine until I click to submit a comment. Here is the newest form code:

    $post_id = $post['post_id'];
              echo '<form method="POST" action='.$_SERVER['PHP_SELF'].' Comment <input type="text" name="comment"/>';
    
              if ($user_ID) {
                   echo '<input type="text" name="author" style="display:none" value="$user_login"/>';
                   echo "logged in as $user_login";
              }
    
              else echo 'Name <input type="text" name="author"/>';
              echo '<input type="text" name="post_id" style="display: none;" value="'.$post_id.'"/>';
              echo '<p><input name="submit" type="submit" id="submit" value="submit" tabindex="5" /></p>';
    
              if (empty($_POST['comment_post_id']) || !(($post_id = (int) $_POST['comment_post_id']) > 0)) {
                   # bad comment_post_id;
              }
    
              $sql=("INSERT INTO wp_comments(comment_author, user_id, comment_content) VALUES ('$_POST[author]','$user_login', '$_POST[comment]')");
              echo '</form>';
    Thread Starter shatterjack

    (@shatterjack)

    I have been testing if the comments are being submitted with this code and variations of it. I have solved the string offset error, but now when I click submit, it just returns the same page as before, and no comment shows up. Also I just noticed, when I log in to the dashboard, it says I have 264 comments approved when there are only 11 appearing. When I click on the comments menu item on the left side in dashboard, it says “listing comments 1-200” but only shows they 11 comments that have displayed on the main page. I’m worried that the test comments are being inserted into the database but in the wrong place. How can I check this?

    Here is the latest form code:

    $post_id = $post['post_id'];
                        echo '<form method="POST" action='.$_SERVER['PHP_SELF'].' Comment <input type="text" name="comment"/>';
    
                        if ($user_ID) {
                             echo '<input type="text" name="author" style="display:none" value="$user_login"/>';
                             echo "  ready to comment as $user_login";
                        }
    
                        else echo 'Name <input type="text" name="author"/>';
                        echo '<input type="text" name="post_id" style="display: none;" value="'.$post_id.'"/>';
                        echo '<p><input name="submit_comment" type="submit" id="submit" value="submit" tabindex="5" /></p>';
    
                        if (empty($_POST['submit_comment']) || !(($post_id = (int) $_POST['submit_comment']) > 0)) {
                             # bad comment_ID;
                        }
    
                        if (isset($_POST['submit_comment'])) {
    
                             $_POST[author]=mysql_real_escape_string($_POST[author]);
                             $user_login=mysql_real_escape_string($user_login);
                             $_POST[comment]=mysql_real_escape_string($POST[comment]);
    
                             mysql_query("INSERT INTO wp_comments(comment_author, u.id, comment_content) VALUES ('$_POST[author]','$user_login', '$_POST[comment]')");
                        }
    
                        echo '</form>';
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘insert comment into database?’ is closed to new replies.