简体   繁体   中英

How to get data from a different database and save it to another database

I don't know if this problem of mine is possible. Is it? I have a library system. I add and edit new books in the Catalog Database. In other words, the Catalog Database is for adding/editing books only. I have another Database (not table) for Borrowing Books. I want to store these books, which are viewed through Catalog DB, to Borrowing DB.

I have a snippet for getting data from Catalog DB

error_reporting(0);
$con = mysql_connect("localhost","root","");
mysql_select_db("catalog", $con);

$acc_number=$_GET["acc_number"];

$query="select * from branch where acc_number = '$acc_number'";
$result=mysql_query($query);

while ($row = mysql_fetch_array($result)) {
  //echo $row[1];
}
<textarea name="title" disabled><?php echo $row[1];?></textarea>

And a button for the submission (store to borrowing database). If button is clicked, it's where my problem occurs. I just got a blank page after submitting it. Here is my process.php:

$con = mysql_connect("localhost","root","");
mysql_select_db("catalog", $con);

$acc_number = $_POST['acc_number'];
$title = $_POST['title'];

$sql = mysql_query("select * from books where acc_number='$acc_number'");

while($row=mysql_fetch_array($sql)){
  $con = mysql_connect("localhost","root","");
  mysql_select_db("borrowing", $con);

  $query="INSERT INTO borrowers (title) VALUES ('$title')";
  mysql_query($query);
  if($query){
    header("Location:../librarysystem/books.php");
  }
}

You need to create two sql connections, one for each DB. Then simply get the data from one DB (perform operations, if required) and write to the second DB.

You have two approaches for this:

  1. Create two separate DB connections and manipulate data there. Passing $conn as connection to MySQL queries will work.
  2. Use the same database using different DB prefixes. Say for example, for first DB it should be

mb_ (Manage Books)

and

bb_ (Borrow Books)

If I were at your place, I would have preferred second approach.

First, I suggest that you use MYSQLI or DO since MYSQL is deprecated. These are suggestions not a fix.

Use only one connect function, you don't need two of them just use the same variable $con.

Add some error checks in there to make sure you are connecting properly

$sql = mysql_query("select * from books where acc_number='$acc_number'") or die ("error message here");

For this

$query="INSERT INTO borrowers (title) VALUES ('$title')";
mysql_query($query);
if($query){
    header("Location:../librarysystem/books.php");
    }

Try

$query=mysql_query("INSERT INTO borrowers (title) VALUES ('$title')") or die("Could not insert...");
    if($query){
        header("Location:../librarysystem/books.php");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM