PHP SQL:如何从一个html表单将数据保存到多个数据库或如何自动将数据从一个数据库复制到另一个数据库 [英] PHP SQL : How to save data to multiple database from one html form OR how to copy data from one database to another database automatically
问题描述
我有一个html表单,例如
I have a html form, say example
<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
。
The form.php is saving that data to database1
.
我想将数据保存到另一个数据库 database2
以及 database1
。
I want that data to be saved to another database database2
along with database1
.
这是可能吗?如果是,那么应该在代码中做什么更改?
Is it possible ?? If yes then what changes should be made in the code ?
如果不可能,则可以从 database1
到 database2
?每当在 database1
中添加一个新行时,它应该自动复制到 database2
。
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
.
我想在两个不同的数据库中有相同的数据。如何实现上述任何一种说法
I want the same data to be in two different database. How can I achieve any of the above said ??
推荐答案
从php你只需要创建一个新的连接到DB。 p>
From php you just have to create new connection to 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();
?>
这篇关于PHP SQL:如何从一个html表单将数据保存到多个数据库或如何自动将数据从一个数据库复制到另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!