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

时间:2015-04-09 15:11:29

标签: php html mysql database

我有一个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>

和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();

?>

form.php将该数据保存到database1

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

有可能吗?如果是,则应在代码中进行哪些更改?

如果不可能,那么是否可以自动将数据从database1复制到database2?每当在database1中添加新行时,它就会自动复制到database2

我希望相同的数据位于两个不同的数据库中。我怎样才能实现上述任何一个?

3 个答案:

答案 0 :(得分:3)

从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();

   ?>

答案 1 :(得分:1)

您需要的是设置复制。

Here是有关复制的官方文档。 Here是一个更简单的分步指南。

如果复制不是你想要的,你可以通过连接到database2以及database1然后在两者上运行一次查询来完成同样的事情。

答案 2 :(得分:0)

您可以使用mysqli::selectDB method之类的内容:

<?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();
?>

查看manual

与你的相似question