2

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.....

1
  • just a heads up, I'd move away from using mysql_ as it's no longer supported. Aim to use the MySQLi extension instead - stackoverflow.com/questions/8891443/… Commented Apr 23, 2014 at 10:31

5 Answers 5

2

Please check,following code may help you.

$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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

if i want to create connection only once from both database and execute quries several time with both of them then?
1

Use dbname.tblname with insert query like

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

Comments

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

Comments

1

I guess this should help. But not tested.

ADVICE: Avoid using mysql_* 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\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();


  }
?>

Comments

0

Thank you all for your answers but i figured it out by myself. U just removed $con.close() from 1st insertion query and removed some concatination from 2nd query and it worked for me atleast.

1 Comment

yes i've figured it out too($con.close())..but forgot to mention ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.