php - 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.
Is there any names/methods to google for?
解决方案
您只需从 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();
?>
推荐阅读
- flutter - Flutter 错误 A RenderFlex 溢出。如何设置图像适合屏幕?
- sql-server - 无法连接到 UWP 上的 SQL Server (SQLEXPRESS)
- google-earth-engine - 无法加载 earthengine-api,即使它已安装
- automation - 如何将日、月和年添加到赛普拉斯中的选定日期
- vue.js - 如何修复 vue2editor 插入链接不起作用的问题?
- java - 带有 3 个 select 语句的存储过程只返回一个表
- linux - UNIX:使用单个 find 命令搜索大于 4 MiB 的文件,然后将输出通过管道传输到排序命令
- google-apps-script - 使用 Google BigQuery / Apps 脚本为插入 Google 表格的数据添加时间戳
- reactjs - BootstrapTable 不接受 data={groupDetails?.group?.data?.contacts} 作为有效数据
- sql - 如何在PL / SQL中声明执行后输出参数(类型num_array)过程结果