php - 在多个数据库中插入数据

原文 标签 php html mysql database

我需要在两个不同的数据库中创建的两个表中输入来自一个html表单的数据,并且我需要在single上完成这项工作提交任何可以实现这一点的建议。
我有以下代码:

<?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语句可以工作…..

最佳答案

请检查,下面的代码可以帮助您。

$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);
}

相关文章:

javascript - this.class与this.getAttribute('class')

html - 如何使用Perl解析无效的HTML?

javascript - PHP,JavaScript和MySQL:变量的更新和传递

html - 字段集宽度父级的100%[重复]

php - 对大量图像进行分类的最佳方法是什么?

mysql - MySql ::选择进入

php - 字符串到数组的转换,array_rand,未设置,然后返回数组到字符串。有捷径吗?

php - Yii2在“ select2”小部件上所需的验证规则

php - 将当前日期与测试成绩一起插入数据库

php - 单击添加按钮后如何将唯一行临时存储到html表中?