【问题标题】:Inserting data in multiple databases在多个数据库中插入数据
【发布时间】:2014-06-08 02:57:11
【问题描述】:

我需要在两个不同数据库中创建的两个表中输入来自单个 html 表单的数据,并且我需要在一次提交任何建议时执行此操作以实现此目的。

我有以下代码:

<?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 "

$table 失败"; $con.close(); } }

?>

还有一个不是单个 echo 语句的东西正在起作用.....

【问题讨论】:

标签: php html mysql database


【解决方案1】:

请检查,以下代码可能对您有所帮助。

$con1 = mysql_connect('localhost', 'user1', 'pass1');
$rv1 = mysql_select_db('db1', $con1);
if(!$con1){
  echo "Connection failed";
}
else{
  mysql_query("INSERT INTO test (name) VALUES('ABC')");
  mysql_close($con1);
}


$con2 = mysql_connect('localhost', 'user2', 'pass2');
$rv2 = mysql_select_db('db2', $con2);
if(!$con2){
  echo "Connection failed";
}
else{
  mysql_query("INSERT INTO test (name) VALUES('ABC')");
  mysql_close($con2);
}

【讨论】:

  • 如果我只想从两个数据库中创建一次连接,然后对它们都执行几次查询?
【解决方案2】:

dbname.tblname 与类似插入查询一起使用

$con1 = mysql_connect('localhost', 'user1', 'pass1');
$rv1 = mysql_select_db('db1', $con1);
if(!$con1){
  echo "Connection failed";
}
else{
  mysql_query("INSERT INTO db1.test (name) VALUES('ABC')");
  mysql_close($con1);
}


$con2 = mysql_connect('localhost', 'user2', 'pass2');
$rv2 = mysql_select_db('db2', $con2);
if(!$con2){
  echo "Connection failed";
}
else{
  mysql_query("INSERT INTO db2.test (name) VALUES('ABC')");
  mysql_close($con2);
}

【讨论】:

    【解决方案3】:
      enter code here`$conn1 = mysql_select_db('db1');
      mysql_open($conn1);
     insert query ->>>
     mysql_close($con1);
    
    
    
     mysql_close($conn2);
    $conn2 = mysql_select_db('db2');
    insert query ->>>
     mysql_close($conn2);
    

    【讨论】:

      【解决方案4】:

      我想这应该会有所帮助。但未经测试。

      建议:避免使用 mysql_* 语句,因为它们在最近的 PHP 版本中已被弃用。学习 mysqli_* Prepared 或 PDO 并开始实施。

      <?php
      if (isset($_POST['submit'])) {
      
      // FIRST DB
      
      $con1 = new mysqli('localhost', 'user', 'password', 'db1');
      
      /* check connection */
      if (mysqli_connect_errno()) {
         printf("Connect failed: %s\n", 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.\n", $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\n", 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.\n", $stmt->affected_rows);
      
      /* close statement and connection */
      $stmt->close();
      
      /* close connection */
      $mysqli->close();
      
      
        }
      ?>
      

      【讨论】:

        【解决方案5】:

        谢谢大家的回答,但我自己想通了。你刚刚从第一个插入查询中删除了$con.close(),并从第二个查询中删除了一些连接,它至少对我有用。

        【讨论】:

        • 是的,我也想通了($con.close())..但忘了提;)
        猜你喜欢
        • 2020-08-26
        • 1970-01-01
        • 2021-09-10
        • 1970-01-01
        • 1970-01-01
        • 2015-07-02
        • 1970-01-01
        • 1970-01-01
        • 2014-12-02
        相关资源
        最近更新 更多