Php 当我按下post按钮时,我的评论不会';我不能发到我的数据库

Php 当我按下post按钮时,我的评论不会';我不能发到我的数据库,php,database,Php,Database,因此,我遇到的问题是,当我点击post按钮时,什么都没有发生,评论也不会显示或进入我的数据库。我已经检查了拼写错误,我相信我都有了 这是我的HTML表单,名为community.php <div class="page-container"> <?php get_total(); require_once 'check_com.php'; ?> <form action=""

因此,我遇到的问题是,当我点击post按钮时,什么都没有发生,评论也不会显示或进入我的数据库。我已经检查了拼写错误,我相信我都有了

这是我的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>")
       });

   })
$(文档)。就绪(函数(){
$(“.child comments”).hide();
$(“a#children”)。单击(函数(){
var section=$(this.attr(“name”);
$(“#C-”+节).toggle();
});
$(“.form submit”)。单击(函数(){
var commentBox=$(“#注释”);
var commentCheck=commentBox.val();
如果(commentCheck=''| | commentCheck==NULL){
addClass(“格式文本错误”);
返回false;
}
});
$(“.form reply”)。单击(函数(){
var replyBox=$(“#新回复);
var replyCheck=replyBox.val();
if(replyCheck=''| | replyCheck==NULL){
addClass(“表单文本错误”);
返回false;
}
});
$(#回复”)。一个(“点击”,函数(){
var comCode=$(this.attr(“名称”);
var parent=$(this.parent();
parent.append(“
”) }); })
检查_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: ");
}

?>

删除
action=“”
或将其更改为
action=“community.php”
您的代码容易受到攻击。您应该通过或使用带有绑定参数的准备语句。有一些很好的示例。添加
会话\u start()
到您的
Check_com.php文件
我在添加session_start()时遇到此错误-注意:session_start():会话已经启动-忽略C:\Xampp\htdocs\website2\Check_com.php中的第2行
<?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;
}
?>