so I have a list of people with their city name and contact numbers in mysql database which is displayed on my website. I want to know which person was contacted by a visitor. Here is a snippet of my code:
<?php
$city = $_POST['city'];
$sql = "SELECT * FROM users WHERE city = '$city'";
$result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['id'];
?>
<tr>
<td class="pl-4"><?php echo "<h3>" .$row['fname']. " " .$row['lname']. "</h3>"; ?>
  <a href="tel: +91<?php echo "" .$row['phnumber']. "" ?>"><BUTTON onClick="contactclick();" class="track btn btn-outline-info p-2"><span class="icon-phone"></span> Contact</BUTTON></a>
  </td>
<script>
function contactclick() {
  <?php
  $sql7 = "SELECT * FROM users WHERE id = '$id' LIMIT 1";
  $result7 = mysqli_query($conn, $sql7);
  $row7 = mysqli_fetch_assoc($result7);
  $firstname = $row7['fname'];
  $lastname = $row7['lname'];
  $city7 = $row7['city'];
  $txt = 'Call received by:'.$firstname.' '.$lastname.' of '.$city7.'';
  $file = fopen('drivers-contacted.txt','a');
  fwrite($file,$txt);  
  ?>
}
</script>
                </tr>
As you can see from the code I have tried making a text file and adding info into that file everytime 'contact' button is clicked. But it adds the name of all people in the list instead of 1 who was actually contacted. How can I solve this? Also, is there a better way to get the information I want, like which user was contacted from my database list?
PS : I'm new to coding
 
     
    