'Didn't update new SQL Database after xmlhttprequest

I am trying to import table from one SQL database to another one by using XMLHttpRequest, it's getting data and insert it in "To DB testdb" with no issue but when I change something in "From DB testdb2" it's update and shows that data on html but didn't update the record of "To DB testdb" where I import original database.

What i want is to update the new database table when i made changes in old database and then they reflect on html page.

<?php
// Connection with From'testdb2' and To'testdb' Database';

// Database From;
$link = mysqli_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect: ' . $link -> error());
}

$link -> select_db("testdb2") or die ("could not open db". $link -> error());


// Database To';
$link2 = mysqli_connect('localhost', 'root', '');
if (!$link2) {
    die('Could not connect: ' . $link2 -> error());
}

$link2 -> select_db("testdb") or die ("could not open db". $link2 -> error());


// GET variable from XMLHTTPREQUEST;
$str =  $_GET['q'];

$myArray = [];
if ($result = $link -> query("SELECT * FROM $str LIMIT 5")) {
        
    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $link2 -> query("INSERT INTO $str (RZ, simei, styp, cimei, ctyp) VALUES ( '".$row['RZ']."', '".$row['simei']."', '".$row['styp']."', '".$row['cimei']."', '".$row['ctyp']."' ) ");
        array_push($myArray, $row);            
    }
      
}
echo json_encode($myArray);

$link2 -> close();
$link -> close();
?>

<script>
function showHint(str) {
  if (str.length == 0) {
    document.getElementById("DataOutput").innerHTML = "";
    return;
  } else {
    const xmlhttp = new XMLHttpRequest();
   
    xmlhttp.onload = function() {

        let data = this.responseText; 
        let jsondata = JSON.parse(data);
        let mapdata = jsondata.map(item => {
            return `<div class="user">
            <span><strong>RZ:</strong> ${item.RZ}</span>
            <span><strong>SIMEI:</strong> ${item.simei}</span>
            <span><strong>STYP:</strong> ${item.styp}</span>
            <span><strong>CIMEI:</strong> ${item.cimei}</span>
            <span><strong>CTYP:</strong> ${item.ctyp}</span>`
        }).toString().replaceAll("\,", "");
        let results = document.getElementById("DataOutput").innerHTML = mapdata; 
        // console.log(jsondata);
    }

  xmlhttp.open("GET", "ajax.php?q=" + str);
  xmlhttp.send();
  }
}
showHint('rz');
setInterval(() => {
 showHint('rz');
    
}, 5000);
</script>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source