douzhun8615 2014-04-23 10:26
浏览 96

在多个数据库中插入数据

I need to enter data from a single html form in two tables which are created in two different databases, and I need to do this on single submit any suggestions what can be done for achieving this.

I have the following code:

<?php

echo "Entering";

$one= mt_rand(1000000000,9999999999);
$two= mt_rand(1000,9999);


echo "<br><br>getting values";

$user= mt_rand(1000000000,9999999999);
$useralias = $one.$two;
$first= $_POST['first_name'];
$last= $_POST['last_name'];
$email=$_POST['email'];
$country=$_POST['country'];
$city = $_POST['city'];
$zipcode = $_POST['zipcode'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$website= $_POST['website'];
$company= $_POST['company'];

echo $first;
echo "<br>".$last;

echo "<br><br>setting database etc one";

$host = "localhost";
$database = "mya2billing";
$table = "cc_card";
$username = "root";
$password = "mehusnain";

echo "<br><br>executing query one";

$con = mysql_connect($host , $username, $password );
if(!$con){
echo "Connection failed";
}
else{
mysql_select_db($database);
$query = "INSERT INTO $table (username, useralias, firstname, lastname, email, country,   city, zipcode, address, phone, fax, company_name, company_website) VALUES   ('$user','$useralias','$first','$last','$email','$country','$city','$zipcode','$address','$ phone','$fax','$website','$company')";

echo $query;

if(mysql_query($query)){
echo "<br><br>Insertion done in $table";
$con.close();
}
else{
echo "<br><br>Failed in $table";
$con.close();
}
}

echo "<br><br>setting databse 2 etc";


$host = "localhost";
$database = "voixe";
$table = "hak_users";
$username = "root";
$password = "mehusnain";

echo "<br><br>executing query 2";

$con = mysql_connect($host , $username, $password );
if(!$con){
echo "Connection failed";
}
else{
mysql_select_db($database);
$query = "INSERT INTO $table (user_login, user_pass, user_nicename, user_email,     display_name) VALUES ('$user','password','$first." ".$last','$email','$first')";

echo $query;

if(mysql_query($query)){
echo "<br><br>Insertion done in $table";
$con.close();
}
else{

echo "

Failed in $table"; $con.close(); } }

?>

One thing more that is not a single echo statement is working.....

  • 写回答

5条回答 默认 最新

  • doonbfez815298 2014-04-23 10:40
    关注

    I guess this should help. But not tested.

    ADVICE: Avoid using mysqli_* statement as they are deprecated in recent PHP versions. Learn mysqli_* prepared or PDO and start implementing.

    <?php
    if (isset($_POST['submit'])) {
    
    // FIRST DB
    
    $con1 = new mysqli('localhost', 'user', 'password', 'db1');
    
    /* check connection */
    if (mysqli_connect_errno()) {
       printf("Connect failed: %s
    ", mysqli_connect_error());
       exit();
    }
    
    $stmt = $mysqli->prepare("INSERT INTO SampleTable VALUES (?)");
    $stmt->bind_param('s', $sample);   // bind $sample to the parameter
    
    // escape the POST data for added protection
    $sample = isset($_POST['sample'])
          ? $mysqli->real_escape_string($_POST['sample'])
          : '';
    
    /* execute prepared statement */
    $stmt->execute();
    
    printf("%d Row inserted.
    ", $stmt->affected_rows);
    
    /* close statement and connection */
    $stmt->close();
    
    /* close connection */
    $mysqli->close();
    
    
    
    //  SECOND DB
    
    $con2 = new mysqli('localhost', 'user', 'password', 'db2');
    
    /* check connection */
    if (mysqli_connect_errno()) {
       printf("Connect failed: %s
    ", mysqli_connect_error());
       exit();
    }
    
    $stmt = $mysqli->prepare("INSERT INTO SampleTable VALUES (?)");
    $stmt->bind_param('s', $sample);   // bind $sample to the parameter
    
    // escape the POST data for added protection
    $sample = isset($_POST['sample'])
          ? $mysqli->real_escape_string($_POST['sample'])
          : '';
    
    /* execute prepared statement */
    $stmt->execute();
    
    printf("%d Row inserted.
    ", $stmt->affected_rows);
    
    /* close statement and connection */
    $stmt->close();
    
    /* close connection */
    $mysqli->close();
    
    
      }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 html5+css有人可以帮吗?
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?