我需要在两个表中输入来自单个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语句的工作.....
答案 0 :(得分:2)
请检查,以下代码可能会对您有所帮助。
$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);
}
答案 1 :(得分:1)
我想这应该会有所帮助。但没有经过测试。
建议:避免使用mysqli_ *语句,因为它们在最近的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();
}
?>
答案 2 :(得分:1)
将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 :(得分:1)
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 :(得分:0)
谢谢大家的答案,但我自己想出来了。你刚刚从第一个插入查询中删除了$con.close()
并从第二个查询中删除了一些连接,它至少对我有效。