How to store form data in 2 different banks?

5

I have a form that the data needs to be saved to a local bank and another bank on a server.

Is there any way to do this without duplicating the "insert" of the code? In the connection file I configured the 2 connections, but only inserted in localhost.

    $host = '***';
    $user= '***';
    $pass = '***';
    $db= '***';
    $con=mysqli_connect($host, $user, $pass, $db);

    $host2='localhost';
    $user2='***';
    $pass2='***';
    $con2=mysqli_connect($host2, $user2, $pass2, $db);
    
asked by anonymous 01.10.2015 / 15:11

2 answers

5

The only way I see it is like this:

$insert = "INSERT INTO TABELA (CAMPO_1, CAMPO_2) VALUES (VALOR_1, VALOR_2)";
mysqli_query($con, $insert);
mysqli_query($con2, $insert);
    
01.10.2015 / 15:25
5
class databaseConfig{

    var $default = array(
        'host'          => '127.0.0.1',
        'login'         => 'root',
        'password'      => '',
        'database'      => 'local',
    );

    var $server = array(
        'host'          => '192.168.100.101',
        'login'         => 'root',
        'password'      => '',
        'database'      => 'server',
    );
}

class SimpleMySqlConnect{

    private $config = array();
    private $connections = array();

    function __construct(){
        if(class_exists("databaseConfig")){
            $this->config = new databaseConfig();
        }

        $connections = get_object_vars($_this->config);
        foreach($connections as $name => $config){
            $this->connections[$name] = new mysqli($config['host'], $config['login'], $config['password'], $config['database']);
        }
    }

    function getConnection($name){
        if(in_array($name, $this->connections)){
            return $this->connections[$name];
        }
    }

    function getConnections(){
        return $this->connections;
    }
}

...

$query = "INSERT INTO myCity (Name, CountryCode, District) VALUES (?,?,?)";
$simpleMySql = new SimpleMySqlConnect();
$connections = $simpleMySql->getConnections();
foreach ($connections as $name => $mySqlClass){

    $stmt = $mySqlClass->prepare($query);
    $stmt->bind_param("sss", $val1, $val2, $val3);

    $val1 = 'Stuttgart';
    $val2 = 'DEU';
    $val3 = 'Baden-Wuerttemberg';

    $stmt->execute();
}

...

Reference

PHP

Obs

I could not test, I do not have Mysql on this computer, if someone finds some error and wants to adjust. Please do it.

    
01.10.2015 / 15:47