如何将数据插入数据库使用Ajax Jquery,php

时间:2017-04-26 05:14:19

标签: javascript php jquery ajax

我已经尝试过很多方法用php jquery,ajax为我的网站建立一个评论系统但是在数据库中插入注释的问题我的代码有什么问题

HTML

 <div id="comments" class="cmt">
    <form method="post"  > 
             <textarea class="textinput"id="comment" rows="5" name="comments" placeholder="Comment Here......"></textarea><br>
                <button type="button" id="comq"name="compost" class="butn2" >post comment</button>
    </form> 
 </div>   

Jquery和ajax

      $(document).ready(function()
     {
       $("#comq").click(function() {
           var comment=$("#comment").val();
           $.ajax({
            cache:false,
            type:"post",
            url:"pnf.php",
            data:{comments:comment},
            success:function(data)
            {
             $('.cmt').html(data);
            }
        });
    });
});

Php(pnf.php)

 if(isset($_POST["compost"])){                
      $comment=$_POST['comments'];        
   {
   $reslt_user= mysqli_query($connection,"SELECT * FROM tbl_users,`queries` where id='".$_SESSION['id']."' AND  qid= '".$qid."' ");
    $row_lat_lng= mysqli_fetch_array($reslt_user);
    $stmt = mysqli_query($connection,"INSERT INTO comments set uid='".$_SESSION['id']."',comments='".$comment."',reply='".$reply."' ,
qid= '".$qid."' ");

    }                             
    if($stmt)
   {
        echo "hello world";
   }  

}

4 个答案:

答案 0 :(得分:1)

试试这个:

HTML

 <div id="comments" class="cmt">
   <textarea class="textinput" id="comment" rows="5" name="comments" placeholder="Comment Here......"></textarea><br>
   <button type="button" id="comq"name="compost" class="butn2" >post comment</button>
 </div>

对于PHP:

 if(isset($_POST["comments"])){

   $comment = $_POST['comments'];

   $reslt_user= mysqli_query($connection,"SELECT * FROM tbl_users,`queries` where id='".$_SESSION['id']."' AND  qid= '".$qid."' ");
   $row_lat_lng= mysqli_fetch_array($reslt_user);
   $stmt = mysqli_query($connection,"INSERT INTO comments set 
    uid='".$_SESSION['id']."',comments='".$comment."',reply='".$reply."' ,
    qid= '".$qid."' ");


  if($stmt)
  {
     echo "hello world";
  }
}

答案 1 :(得分:0)

您的类型有错误:

     if(isset($_POST["compost"])){

      $comment=$_POST['comments'];

我的意思是$_POST["compost"]$_POST["compost"]是不同的变量

答案 2 :(得分:0)

首先只是设置$ _POST注释以检查你是否有评论值

if(isset($_POST["comments"])){

  $comment=$_POST['comments'];

  $reslt_user= mysqli_query($connection,"SELECT * FROM tbl_users,`queries` where id='".$_SESSION['id']."' AND  qid= '".$qid."' ");
  $row_lat_lng= mysqli_fetch_array($reslt_user);
  $stmt = mysqli_query($connection,"INSERT INTO comments set uid='".$_SESSION['id']."',comments='".$comment."',reply='".$reply."' ,qid= '".$qid."' ");

  if($stmt)
  {
      return true;
  }
}

第二,你可以在ajax调用成功之后id到你的表单重置

<form id="myForm"> // add ID to your form

success:function(data){
     if(data == true){
        $('#myForm')[0].reset(); //to reset your form after success 
        $('.cmt').html(data);
     } else{
          //do the error msg
     }
}

答案 3 :(得分:0)

我正在分享工作代码段。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" ></script>
</head>
<body>

<div id="comments" class="cmt">
    <form method="post" onsubmit="submitComment(); return false;">
        <textarea class="textinput" id="comment" rows="5" name="comments" placeholder="Comment Here......"></textarea><br>
        <button type="submit" id="comq"name="compost" class="butn2" >post comment</button>
    </form>
    <p id="message" ></p>
</div>

<script>

    function submitComment() {

        var comment = $("#comment").val();
        $.ajax({
            cache: false,
            type: "post",
            url: "pnf.php",
            data:{
                comment:comment
            },
            success:function(data) {
                $('#message').html(data);
            }
        });

        return false;
    }
</script>

</body>
</html>

和PHP脚本

<?php

$connection = mysqli_connect("localhost","my_user","my_password","my_db");

if(mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if(isset($_POST["comment"])) {

  $comment = mysqli_real_escape_string($connection, $_POST['comment']);

  $sql = "INSERT INTO `comments` (`comment`) VALUES ('".$comment."')";

  $res = mysqli_query($connection, $sql);

  if($res) {
    echo "success";
  } else {
    echo "error";
  }

}