Q: MySQL - How to control `numeric` column of one table from another table I have the following:
book(book_id,book_name,quantity)
donor(book_id)
acquisition(donor_id,book_id,booksDonated)

When I insert a data in acquisition, the value of booksDonated must update to the given book_id's quantity.
I tried learning triggers, but it turns out it cannot pass variable, since i am working in PHP. How can I insert and update(with the given data of the table) at the same time. 
PS. Please add a query of it if it's something complex.

A: Here's a more complete answer. First, you need to create the following table structure in your database (creating tables, primary keys and autoincrement fields).
CREATE TABLE IF NOT EXISTS `acquisition` (
  `acquistion_id` bigint(20) NOT NULL,
  `donor_id` bigint(20) NOT NULL,
  `book_id` bigint(20) NOT NULL,
  `booksDonated` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `book` (
  `book_id` bigint(20) NOT NULL,
  `book_name` varchar(255) NOT NULL,
  `quantity` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `donor` (
  `donor_id` bigint(20) NOT NULL,
  `name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `acquisition` ADD PRIMARY KEY (`acquistion_id`);

ALTER TABLE `book` ADD PRIMARY KEY (`book_id`);

ALTER TABLE `donor` ADD PRIMARY KEY (`donor_id`);

ALTER TABLE `acquisition` MODIFY `acquistion_id` bigint(20) NOT NULL AUTO_INCREMENT;

ALTER TABLE `book` MODIFY `book_id` bigint(20) NOT NULL AUTO_INCREMENT;

ALTER TABLE `donor` MODIFY `donor_id` bigint(20) NOT NULL AUTO_INCREMENT;

Now, based on this structure, you can use the following functions to 
Add a book 
Add a donor (2 different donor's in the example) 
Add a book's acquisition
function addBook($con, $name)
{
    $id = -1;
    $name = mysqli_real_escape_string($con, $name);
    $query = "INSERT INTO `book` (`book_id`, `book_name`, `quantity`) VALUES (NULL, '$name', '0');";
    mysqli_query($con, $query);
    if (mysqli_error($con) == 0)
    {
    $id = mysqli_insert_id($con);
    }
    return $id;
}
function addDonor($con, $name)
{
    $id = -1;
    $name = mysqli_real_escape_string($con, $name);
    $query = "INSERT INTO `donor` (`donor_id`, `name`) VALUES ('1', '$name');";
    mysqli_query($con, $query);
    if (mysqli_error($con) == 0)
    {
    $id = mysqli_insert_id($con);
    }
    return $id;
}
function addAcquisition($con, $book_id, $donor_id, $quantity)
{
    $id = 0;
    $book_id = intval($book_id);
    $donor_id = intval($donor_id);
    $quantity = intval($quantity);
    $query = "INSERT INTO `acquisition` (`acquistion_id`, `donor_id`, `book_id`, `booksDonated`) VALUES (NULL, '{$donor_id}', '{$book_id}', '{$quantity}');";
    mysqli_query($con, $query);
    if (mysqli_error($con) != 0)
    {
    $id = -1;
    }

//This SQL updates the current book count with the newly donated quantity, so they stay in sync.

    $query = "UPDATE `book` SET `quantity` = (`quantity` + '{$quantity}') WHERE `book_id` = '{$book_id}';";
    mysqli_query($con, $query);

    if (mysqli_error($con) == 0)
    {
    $id = mysqli_insert_id($con);
    }
    return $id;
}

Now, using the functions, you can create a book, one or more donors and their acquisitions.
// Fill your DB values here
$host = '';
$username = '';
$password = '';
$database = '';

// Connect to the DB
$con = mysqli_connect($host, $username, $password, $database);

// Add a book. $book_id contains the new book's ID
$book_id = addBook($con, 'This is the book\'s name');

// Add a donor. $book_id contains the new donor's ID
$donor1_id = addDonor($con, 'This is the first Donor\'s name');

// Add another book. $book_id contains the second donor's ID
$donor2_id = addDonor($con, 'This is the second Donor\'s name');

addAcquisition($con, $book_id, $donor1_id, 5);
addAcquisition($con, $book_id, $donor2_id, 10);

// After running this code, there'll be 15 in the quantity field (5 from Donor 1 + 10 from Donor 2) and 2 acquistion records


A: You can use an UPDATE query, like the following:
UPDATE book SET Quantity = Quantity + 'BOOKSDONATED' WHERE book_id = ID
This way, you add BOOKSDONATED books (the number of books the donor is giving) to the existing quantity of books with book_id = ID
You then insert a record on the acquisition table using the books_I'd and donor_I'd.
