0

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>
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Feb 05 '22 at 16:17

0 Answers0