Php 将数据插入mysql数据库时出现问题

Php 将数据插入mysql数据库时出现问题,php,mysql,ajax,Php,Mysql,Ajax,我正在创建一个简单的多聊天web应用程序。之前我已经创建了这个系统,但后来由于这个原因,我在源代码中做了一些更改 当我输入输入时,它会在数据库中多次存储相同的数据,同时重复的数据会自动递增。请任何人帮助我解决此问题 这是我的索引页 <?php include 'db.php';?> <!DOCTYPE html> <html> <head> <style> #wraper{ height:550px; wi

我正在创建一个简单的多聊天web应用程序。之前我已经创建了这个系统,但后来由于这个原因,我在源代码中做了一些更改 当我输入输入时,它会在数据库中多次存储相同的数据,同时重复的数据会自动递增。请任何人帮助我解决此问题

这是我的索引页

<?php include 'db.php';?>
<!DOCTYPE html>
<html>
<head>  


<style>
   #wraper{
    height:550px;
    width:100%;
   }
   #stage{
    height:450px;
    width:100%;
    background-color:black;
    color:white;
    overflow:auto;

   }
   #pen
   {
    height:100px;
    width:100%;
    background-color:red;
   }

   </style>
  <title>forum</title>  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
  </head> 
<body>
  <div id="wraper">
    <div id="stage">message goes here</div>
    <div id="pen">

  <br>
  <form id="image_form" method="post" enctype="multipart/form-data">
     <input type="hidden" name="action" id="action" value="insert" />
     <input type="hidden" name="image_id" id="image_id" />
     <input type="hidden" name="user_id" id="user_id" value="<?php session_start(); echo $_SESSION['si']; ?>" />
     <input type="text"
       cols="40" 
       rows="5" 
       style="width:200px; height:50px;" 
       name="description" 
       id="description" 
       placeholder="say some thing"
       autocomplete="off" required />
       <input type="hidden" name="clock" id="clock" value="<?php echo date('Y-m-d H:i:s'); ?>" readonly="readonly" />
       <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-info" />
 </form>
 </div></div>
 </body>
 </html>
<script>  
$(document).ready(function display_msg(){
   var action = "fetch";
   $.ajax({
   url:"forum_action.php",
   method:"POST",
   data:{action:action},
   success:function(data)
   {
    $('#stage').html(data);
    var objDiv = document.getElementById("stage");
        objDiv.scrollTop = objDiv.scrollHeight;
  }
  });


$('#image_form').submit(function(event){
     event.preventDefault();
     $.ajax({
     url:"forum_action.php",
     method:"POST",
     data:new FormData(this),
     contentType:false,
     processData:false,
     success:function(data)
     {
      //alert(data);
      $('#image_form')[0].reset();
      display_msg();
     }
    })
   });
  });
</script>

#包装工{
高度:550px;
宽度:100%;
}
#舞台{
高度:450px;
宽度:100%;
背景色:黑色;
颜色:白色;
溢出:自动;
}
#笔
{
高度:100px;
宽度:100%;
背景色:红色;
}
论坛
信息就在这里


您面临这个问题是因为,在您不知情的情况下,您刚刚创建了递归函数,即
display\u msg
函数。为了避免这种行为,您应该将表单提交事件处理程序放在
document.ready
事件处理程序之外

//display\u msg函数的实现
函数显示\u msg(){
var action=“fetch”;
$.ajax({
url:“forum_action.php”,
方法:“张贴”,
数据:{action:action},
成功:功能(数据)
{
$('#stage').html(数据);
var objDiv=document.getElementById(“阶段”);
objDiv.scrollTop=objDiv.scrollHeight;
}
}
//加载文档时,执行display_msg函数
$(文档).ready(显示消息);
//将提交事件侦听器附加到#图像_表单
$(“#图像_表单”).submit(函数(事件){
event.preventDefault();
$.ajax({
url:“forum_action.php”,
方法:“张贴”,
数据:新表单数据(本),
contentType:false,
processData:false,
成功:功能(数据)
{
//警报(数据);
$(“#图像形式”)[0]。重置();
display_msg();
}
})
});
现在,它不应该一次又一次地插入相同的数据,您应该在
body
结束标记之前插入该脚本,以确保页面中的所有元素都已加载并可访问

附言:你把纯
JavaScript
jQuery
混为一谈,这不是明智之举 选择,您应该只使用其中一个


希望我能进一步推动您。

您的脚本非常开放,甚至可以在
MYSQLI\ucode>或
PDO
API中使用。您应该在服务器端(PHP)或客户端(javascript)或两者中使用日志或调试。@VenkatCpr我的回答对您有帮助吗?
<!DOCTYPE html>
<html>
<head>
  <title></title>
  <style>
  #container
    {
      height:70px;
      width:50%;
      color:white;

    }
    .usr_id
    {
      background-color:blue;
      height:20px;
      width:40%;
      position:relative;
      float:left;
      border-radius: 15px 0 0 0;
    }
    .msg
    {
    background-color:green;
    height:30px;
      width:100%; 
     position:relative;
      float:left;
      border-radius:0 0 15px 15px;  
    }
    .clock
    {
      background-color:purple;
      height:20px;
      width:60%;
      position:relative;
      float:left;
      border-radius:0 15px 0 0;
      text-align: right;
    }
  </style>
</head>
<body>
<?php
//action.php
if(isset($_POST["action"]))
{
 $connect = mysqli_connect("localhost", "root", "", "flash");

                                          //INSERTING MESSSA

 if($_POST["action"] == "insert")
 {
  $name=$_POST['user_id'];
  $des= $_POST['description'];
  $clock=$_POST['clock'];
  $query = "INSERT INTO forum(user_id,description,clock) VALUES ('$name','$des','$clock')";
  if(mysqli_query($connect, $query))
  {
   echo 'Data Inserted into Database';
  }

 }
                                      // FETCHING MESSAGES
  if($_POST["action"] == "fetch")
 {
  $query = "SELECT * FROM forum ORDER BY id";
  $result = mysqli_query($connect, $query);
  $output = '
   <div>
  ';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '
   <div id="container">
<div class="usr_id">'.$row["user_id"].'</div>
<div class="clock">'.$row["clock"].'</div>
<div class="msg">'.$row["description"].'</div>
</div><br>
   ';
  }
  $output .= '</div>';
  echo $output;
 }


}
?>

</body>
</html>