PHP SQL:如何从一个html表单将数据保存到多个数据库,或者如何自动将数据从一个数据库复制到另一个数据库

时间:2022-07-17 19:20:17

I have a html form, say example

例如,我有一个html表单。

<form action="form.php" method="post">
First name:<br>
<input type="text" id="fname" name="fname">
<br>
Last name:<br>
<input type="text" id="lname" name="lname">
<br><br>
<input type="submit" value="Submit">
</form>

and form.php

和form.php

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

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->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;
}

$conn->close();

?>

The form.php is saving that data to database1 .

表单。php将数据保存到database1。

I want that data to be saved to another database database2 along with database1.

我希望将这些数据与database1一起保存到另一个数据库数据库中。

Is it possible ?? If yes then what changes should be made in the code ?

是否有可能?如果是,那么应该在代码中做什么更改?

If it is not possible then is it possible to copy data from database1 to database2 automatically? Whenever a new row is added in database1 then it should automatically copied to database2.

如果不可能,那么是否可以自动将数据从database1复制到database2 ?无论何时在database1中添加新行,它都应该自动复制到database2。

I want the same data to be in two different database. How can I achieve any of the above said ??

我希望相同的数据放在两个不同的数据库中。我如何做到以上所说的任何一种?

3 个解决方案

#1


1  

From php you just have to create new connection to DB.

从php中,您只需创建到DB的新连接。

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


   $servernameS = "localhost";
   $usernameS = "database2";
   $passwordS = "xxxxxxxx";
   $dbnameS = "database2";

   // Create connection
   $conn = new mysqli($servername, $username, $password, $dbname);
   $connS = new mysqli($servernameS, $usernameS, $passwordS, $dbnameS);

   // Check connection
  if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
   }
   if ($connS->connect_error) {
       die("Connection failed: " . $connS->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 ($connS->query($sql) === TRUE) {
     echo "Successfully Saved";

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

   ?>

#2


1  

What it sounds like you need is to setup replication.

听起来您需要的是设置复制。

Here is the official documentation on replication. Here is a simpler step-by-step guide setting it up.

这是关于复制的官方文档。这里有一个更简单的步骤指南。

If replication isn't what you wanted, you could accomplish the same thing by connecting to database2 in addition to database1 then running the query once on both.

如果复制不是您想要的,那么除了database1之外,您还可以通过连接database2来完成相同的任务,然后在这两个服务器上运行查询一次。

#3


0  

You can use something like using mysqli::selectDB method:

可以使用mysqli::selectDB方法:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* return name of current default database */
if ($result = $mysqli->query("SELECT DATABASE()")) {
    $row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();
}

/* change db to world db */
$mysqli->select_db("world");

/* return name of current default database */
if ($result = $mysqli->query("SELECT DATABASE()")) {
    $row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();
}

$mysqli->close();
?>

Check out the manual.

检查手册。

Similar question as yours at SO.

类似的问题。

#1


1  

From php you just have to create new connection to DB.

从php中,您只需创建到DB的新连接。

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


   $servernameS = "localhost";
   $usernameS = "database2";
   $passwordS = "xxxxxxxx";
   $dbnameS = "database2";

   // Create connection
   $conn = new mysqli($servername, $username, $password, $dbname);
   $connS = new mysqli($servernameS, $usernameS, $passwordS, $dbnameS);

   // Check connection
  if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
   }
   if ($connS->connect_error) {
       die("Connection failed: " . $connS->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 ($connS->query($sql) === TRUE) {
     echo "Successfully Saved";

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

   ?>

#2


1  

What it sounds like you need is to setup replication.

听起来您需要的是设置复制。

Here is the official documentation on replication. Here is a simpler step-by-step guide setting it up.

这是关于复制的官方文档。这里有一个更简单的步骤指南。

If replication isn't what you wanted, you could accomplish the same thing by connecting to database2 in addition to database1 then running the query once on both.

如果复制不是您想要的,那么除了database1之外,您还可以通过连接database2来完成相同的任务,然后在这两个服务器上运行查询一次。

#3


0  

You can use something like using mysqli::selectDB method:

可以使用mysqli::selectDB方法:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* return name of current default database */
if ($result = $mysqli->query("SELECT DATABASE()")) {
    $row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();
}

/* change db to world db */
$mysqli->select_db("world");

/* return name of current default database */
if ($result = $mysqli->query("SELECT DATABASE()")) {
    $row = $result->fetch_row();
    printf("Default database is %s.\n", $row[0]);
    $result->close();
}

$mysqli->close();
?>

Check out the manual.

检查手册。

Similar question as yours at SO.

类似的问题。