sql >> Base de Datos >  >> RDS >> Mysql

PHP SQL:cómo guardar datos en varias bases de datos desde un formulario html O cómo copiar datos de una base de datos a otra base de datos automáticamente

Desde php solo tienes que crear una nueva conexión a 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();

   ?>