当我按下发布按钮时,我的评论没有发布到我的数据库

时间:2018-04-04 14:39:41

标签: php database

所以我遇到的问题是当我点击帖子按钮时没有任何反应,评论不会显示或转到我的数据库。我检查了拼写错误,并相信我已经完成了所有这些。

这是我的HTML表单,名为community.php

<div class="page-container">
        <?php 
            get_total();
            require_once 'check_com.php';
        ?>

   <form action="" method="post" class="main">
        <label>Enter a Brief Comment</label>
       <textarea class="form-text" name="comment" id="comment"></textarea>
       <br />
       <input type="submit" class="form-submit" name="new_comment" value="Post">

   </form>
        <?php get_comments(); ?>

        </div>

这是我的js脚本,名为global.js

$(document). ready(function() {
    $(".child-comments").hide();

$("a#children").click(function() {
    var section = $(this).attr("name");
    $("#C-" + section).toggle();
});

$(".form-submit").click(function() {
    var commentBox=  $("#comment");
    var commentCheck=  commentBox.val();
    if(commentCheck == '' || commentCheck == NULL) {
        commentBox.addClass("form-text-error");
        return false;
    }

  });

 $(".form-reply").click(function() {
    var replyBox=  $("#new-reply);
    var replyCheck=  replyBox.val();
    if(replyCheck == '' || replyCheck == NULL) {
        replyBox.addClass("form-text-error");
        return false;
    }

   });

    $("a#reply").one("click", function() {
        var comCode = $(this).attr("name");
        var parent = $(this).parent();

        parent.append("<br / ><form actions='' method='post'><textarea class='form-text' name='new-reply' id='new-reply' required='required'></textarea><input type='hidden' name='code' value='"+comCode"' /><input type='submit' class='form-submit' id='form-reply' name='new_reply' value='Reply'/></form>")
       });

   })

Check_com.php文件

<?php
// new comment fucntion
if(isset($_POST['new_comment'])) {
    $new_com_name = $_SESSION['user'];
    $new_com_text = $_POST['comment'];
    $new_com_date = date('Y-m-d H:i:s');
    $new_com_code = generateRandomString();

    if(isset($new_com_text)) {
        mysqli_query($conn, "INSERT INTO `parents` (`user`, `text`, `date`, `code`) VALUES ('$new_com_name', '$new_com_text', '$new_com_date', '$new_com_code')"); 
    }
    header ("Location: ");
}
// new reply
if(isset($_POST['new_reply'])) {
    $new_reply_name = $_SESSION['user'];
    $new_reply_text = $_POST['new-reply'];
    $new_reply_date = date('Y-m-d H:i:s');
    $new_reply_code = $_POST('code');

    if(isset($new_reply_text)) {
        mysqli_query($conn, "INSERT INTO `children` (`user`, `text`, `date`, `par_code`) VALUES ('$new_reply_name', '$new_reply_text', '$new_reply_date', '$new_reply_code')"); 
    }
    header ("Location: ");
}

?>

Functions.php文件

<?php 
session_start();
$_SESSION['user'] = 'Admin';

function get_total() {
    require 'includes/dbh.inc.php';
    $result = mysqli_query($conn, "SELECT * FROM `parents` ORDER BY `date` DESC");
    $row_cnt = mysqli_num_rows($result);
    echo '<h1>All Comments ('.$row_cnt.')</h1';
}

function get_comments() {
    require 'includes/dbh.inc.php';
    $result = mysqli_query($conn, "SELECT * FROM `parents` ORDER BY `date` DESC");
    $row_cnt = mysqli_num_rows($result);

    foreach($result as $item) {
        $date = new dateTime($item['date']);
        $date = date_format($date, 'M j, Y | H:i:s');
        $user = $item['user'];
        $comment = $item['text'];
        $par_code = $item['code'];

        echo '<div class="comment" id="'.$par_code.'">'
                .'<p class="user">'.$user.'</p>&nbsp;'
                .'<p class="time">'.$date.'</p>'
                .'<p class="comment-text">'.$comment.'</p>'
                .'<a class="link-reply" id="reply" name="'.$par_code.'">Reply</a>';
        $chi_result = mysqli_query($conn, "SELECT * FROM `children` WHERE `par_code`='$par_code' ORDER BY `date` DESC");
        $chi_cnt = mysqli_num_rows($chi_result);

        if($chi_cnt == 0){
            }else {
            echo '<a class="link-reply" id="children" name="'.$par_code.'"><span id="tog_text">replies</span> ('.$chi_cnt.')</a>'
                .'<div class="child-comments" id="C-'.$par_code.'">';
            foreach ($chi_result as $com) {
                $chi_date = new dateTime($com['date']);
                $chi_date = date_format($chi_date, 'M j, Y | H:i:s');
                $chi_user = $com['user'];
                $chi_com = $com['text'];
                $chi_par = $com['par_code'];

                 echo '<div class="child" id="'.$par_code.'-C">'
                    .'<p class="user">'.$chi_user.'</p>&nbsp;'
                    .'<p class="time">'.$chi_date.'</p>'
                    .'<p class="comment-text">'.$chi_com.'</p>'
                   .'</div>';
            }
            echo '</div>';

        }
        echo '</div>';

    }
}

function generateRandomString($length = 6) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $characterLength = strlen($characters);
    $randomString = '';

    for($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $characterLenght - 1)];
    }
    return $randomString;
}
?>

1 个答案:

答案 0 :(得分:-1)

删除action=""或将其更改为action="community.php"