首页 > 解决方案 > How to transfer data from DB1 to DB2 (different network/server)

问题描述

I want to set up a website with a form in it. The form will transfer the data to the DB, but I think it is not safe to let the personal data in the DB which is external reachable. So I thought I should transfer the data via PHP from the DB1(server1 - external reachable) to DB2(server2 - only internal reachable).

The following picture should help to know what I am searching for.

diagram

Is there any names/methods to google for?

标签: phpdatabase

解决方案


您只需从 php 为 DB2 创建一个新连接。

<?php
   $servername = "localhost";
   $username = "database1";
   $password = "xxxxxxxx";
   $dbname = "database1";


   $servername2 = "localhost";
   $username2 = "database2";
   $password2 = "xxxxxxxx";
   $dbname2 = "database2";

   // Create connection
   $conn = new mysqli($servername, $username, $password, $dbname);
   $conn2 = new mysqli($servername2, $username2, $password2, $dbname2);

   // Check connection
  if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
   }
   if ($conn2->connect_error) {
       die("Connection failed: " . $conn2->connect_error);
   }

   //escape variables for security
   $fname = mysqli_real_escape_string($conn, $_POST['fname']);
   $lname = mysqli_real_escape_string($conn, $_POST['lname']);

   $sql = "INSERT INTO mytable (fname,lname) 
   VALUES ('$fname','$lname')";

    if ($conn->query($sql) === TRUE) {
     echo "Successfully Saved";

    } else {
      echo "Error: Go back and Try Again ! " . $sql . "<br>" . $conn->error;
    }

    if ($conn2->query($sql) === TRUE) {
     echo "Successfully Saved";

    } else {
      echo "Error: Go back and Try Again ! " . $sql . "<br>" . $conn2->error;
    }
    $conn->close();
    $conn2->close();

   ?>

推荐阅读