简体   繁体   English

Ajax数据插入数据库不起作用

[英]Ajax data insert into database not working

I am trying to build a comment system for my website using Ajax, jQuery and PHP. 我正在尝试使用Ajax,jQuery和PHP为我的网站构建评论系统。 My site has the lot of queries, how can I submit comments every query separate? 我的网站上有很多查询,如何在每个查询中分别提交评论?

Ajax code Ajax代码

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

when I submit the comments, comments only inserted but query (qid) not inserted in DB (database table) 当我提交评论时,评论仅插入而查询(qid)未插入数据库(数据库表)

php code PHP代码

 if(isset($_POST["comments"])){         
    $comment=$_POST['comments'];
    $qid= $_POST['qid'];
    $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."' ");

Html code HTML代码

     <div id="comments" class="cmt" >
    <input class="commentbox"id="comment"name="comments"placeholder="Comment 
               Here" maxlength="50">
    <input type="hidden"id="qid "name="qid">
     <button type="button" id="comq" name="compost" class="butn2" value="submit">
    </button>
                        </div>

How to post comments as per queries (how to insert)? 如何根据查询发布评论(如何插入)?

See this line here, 在这里看到这行,

<input type="hidden" id="qid "name="qid">
                        ^^^^
  • Misplaced closing " for id attribute. id属性的结尾处放错了"
  • value attribute is missing from the hidden input element. 隐藏的输入元素中缺少value属性。

So the hidden input element should be like this: 因此隐藏的输入元素应如下所示:

<input type="hidden" id="qid" name="qid" value="SOME VALUE" />

Sidenote: Learn about prepared statement because right now your queries are susceptible to SQL injection attack. 旁注:了解准备好的语句,因为现在您的查询容易受到SQL注入攻击。 Also see how you can prevent SQL injection in PHP . 另请参阅如何防止在PHP中进行SQL注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM