gpt4 book ai didi

php - MySQL-如何从另一个表控制一个表的 `numeric` 列

转载 作者:行者123 更新时间:2023-11-29 12:22:37 24 4
gpt4 key购买 nike

我有以下内容:

book(book_id,book_name,quantity)
donor(book_id)
acquisition(donor_id,book_id,booksDonated)

当我在采集中插入数据时,booksDonated 的值必须更新为给定 book_idquantity。我尝试学习触发器,但事实证明它无法传递变量,因为我在 PHP 中工作。
如何同时插入和更新(使用表的给定数据)。
附言。如果比较复杂,请添加查询。

最佳答案

这是一个更完整的答案。首先,您需要在数据库中创建以下表结构(创建表、主键和自增字段)。

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;

现在,基于这个结构,你可以使用以下函数

添加一本书添加捐赠者(示例中为 2 个不同的捐赠者)添加图书购买

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

现在,使用这些功能,您可以创建一本书、一个或多个捐赠者及其收购的元素。

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

关于php - MySQL-如何从另一个表控制一个表的 `numeric` 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28794474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com