用户之间的聊天系统

I have been trying to create a chatting system using php+ ajax + mysql.

<?php
  session_start();
?>
<html>  
  <head>  
       <title>Live Table Data Edit</title>  
       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
       <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
  </head>  
  <body>  
       <div class="container">  
            <br />  
            <br />  
            <br />  
            <div class="table-responsive">  
                 <h3 align="center">You Are : <?php echo $_SESSION['name']; 
                ?></h3><br />  
                 <div id="live_data"></div>                 
            </div>  
            <div id="messages"></div> 
            <div class="area" style="display:none">
            <textarea id="text" name="text"></textarea>
            <input type="submit" id="sub" name="sub" value="Send" />
            </div>
       </div>  

  </body>  
  </html>  

   <script>  
$(document).ready(function() {
  function fetch_data() {
    $.ajax({
      url: "select.php",
      method: "POST",
      success: function(data) {
        $('#live_data').html(data);
      }
    });
  }
  fetch_data();

  $(document).on('click', '.first_name', function() {
    var id = $(this).data("id1");

    function fetch_chat() {
      $.ajax({
        url: "fetch_chat.php",
        method: "POST",
        data: {
          id: id
        },
        dataType: "text",
        success: function(data) {
          $('#messages').html(data);
          $("div.area").show();
        }

      });
    }

    fetch_chat();
    $("#sub").click(function() {
      var text = $("#text").val();
      $.post('insert_chat.php', {
        id: id,
        msg: text
      }, function(data) {
        $("#messages").append(data);
        $("#text").val('');

      });
      alert(text);
    });
  });
});

but the trouble is that when I try to insert a new message in mysql the messages is send to all the users in which i have clicked.

the fetch_chat.php

<html>
<head>
</head>
<body>
<?php
session_start();
$con=mysqli_connect('localhost','root','','test');
$id= $_POST['id'];
$sql="select * from users where id='$id'";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_array($res);
$user_to= $row['name'];
$sql1="select * from chats where user_from='$_SESSION[name]' AND 
user_to='$user_to' OR user_to='$_SESSION[name]' AND user_from='$user_to' 
order  by id";
$res1=mysqli_query($con,$sql1);
if(mysqli_num_rows($res1)>0){
    while($row=mysqli_fetch_array($res1)){
?>
        <b> <?php echo $row['user_from']; ?>:</b> 
        <?php echo $row['msg']; ?><br /><br />
        <?php
    }
}
else
    echo "No msgs <br />";
?>

</body>
</html>

and insert_chat.php is:

<html>
<head>
</head>
<body>
<?php
session_start();
$con=mysqli_connect('localhost','root','','test');
$id= $_POST['id'];
$msg=$_POST['msg'];
$sql="select * from users where id='$id'";
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_array($res);
$user_to= $row['name'];

$sql1="insert into chats(user_from,user_to,msg) 
values('$_SESSION[name]','$user_to','$msg')";
$res1=mysqli_query($con,$sql1);
if($res1){
?>
    <b> <?php echo $_SESSION['name']; ?>:</b> 
    <?php echo $msg; ?><br /><br />
<?php   
}
?>
</body>
</html>

I don't know where I am going wrong. Please help me with this.

For example, the list of users is:

(1) user a

(2) user b

(3) user c

So at first I clicked a then I changed my mind to select b and send the text. But there arises the problem: the data gets inserted for both a and b like I am texting both of them at the same time.

You keep on adding more and more "click" event handlers to "sub", every time you click on "first_name". So then each time you click sub again, it runs all the previous event handlers again and again. So you get the current message, but sent to all users you previously clicked on since you loaded the page.

$("#sub").click(function() { 

needs to be

$("#sub").off("click").on("click", function() { 

so that it removes previous handlers before you define the new version with new data.