将数据插入两个不同的数据库中

时间:2017-03-19 20:15:18

标签: php mysqli

我有两个不同的数据库,我想使用一个脚本PHP页面输入信息。下面是我尝试使用的代码,但没有任何内容插入到DB的所有内容中

<?php
// Record to Multiple MySQL Databases using MySQLi
// Recovery Assist SITLOK System (CONN)
$host = "localhost"; // Host name 
$username = "****"; // Mysql username 
$password = "****"; // Mysql password 
$db_name = "****"; // Database name 

// Recovery Assist eTrack Platform System (CONN2)
$host1 = "localhost"; // Host name 
$username1 = "****"; // Mysql username 
$password1 = "****"; // Mysql password 
$db_name1 = "****"; // Database name 

    date_default_timezone_set('Africa/Johannesburg');
    $today = date("Y-m-d H:i:s");
    $date = date("Y-m-d") ;
    $time = date("H:i:s");

// Create connection
$conn = new mysqli($host, $username, $password, $db_name);
$conn2 = new mysqli($host1, $username1, $password1, $db_name1);

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$name = $_POST['name']; 
$surname = $_POST['surname'];
$msisdn = $_POST['msisdn'];
$group = $_POST['group'];
$msg = $_POST['msg'];
$email = $_POST['email'];
$active = $_POST['active'];
$pass = $_POST['password'];
$company = $_POST['company'];
$position = $_POST['position'];

        $conn->query = "INSERT INTO sitelok SET
                name = '".$name." ".$surname."',
                email = '".$email."',
                custom1 = '".$msisdn."',
                username = '".$msisdn."',
                usergroup = '".$group."';
                enabled = '".$active."';
                passphrase = '".$pass."';
                custom2 = '".$company."';
                custom6 = '".$position."'";

        $conn2->query = "INSERT INTO members SET
                ussd_name = '".$name."';
                ussd_surname = '".$surname."';
                member_msisdn = '".$msisdn."';
                ussd_office = '".$group."';
                ussd_msg = '".$msg."';
                ussd_email = '".$email."';
                ussd_dealer = '".$company."'";

if (mysqli_query($con,$con->query))
    {
    if (mysqli_query($con2,$con2->query))
    {
    }
    else
    {
        echo "Error: System did not Load.<br/>";
    }
}
mysqli_close($con);
?> 

我不确定我是否连接到数据库,因为我的屏幕上没有错误

我发现这是有效的,因为上面的代码有很多错误和错误

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
ini_set('display_errors',0);
ini_set('log_errors',1);

// Create connection
$conn = new mysqli($host2, $username2, $password2, $db_name2);
$conn2 = new mysqli($host1, $username1, $password1, $db_name1);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
ini_set('display_errors',1);

$name = $_POST['name']; 
$surname = $_POST['surname'];
$msisdn = $_POST['msisdn'];
$group = $_POST['group'];
$msg = $_POST['msg'];
$email = $_POST['email'];
$active = $_POST['active'];
$pass = $_POST['password'];
$company = $_POST['company'];
$position = $_POST['position'];

        $sql = "INSERT INTO sitelok SET
                name = '".$name." ".$surname."',
                email = '".$email."',
                custom1 = '".$msisdn."',
                username = '".$msisdn."',
                usergroups = '".$group."',
                enabled = '".$active."',
                passphrase = '".$pass."',
                custom2 = '".$company."'";

        $sql2 = "INSERT INTO members SET
                ussd_name = '".$name."',
                ussd_surname = '".$surname."',
                member_msisdn = '".$msisdn."',
                ussd_office = '".$group."',
                ussd_msg = '".$msg."',
                ussd_email = '".$email."',
                ussd_dealer = '".$company."'";


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

if ($conn2->query($sql2) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql2 . "<br>" . $conn2->error;
}

$conn->close();
$conn2->close();

0 个答案:

没有答案