[英]How to update a single column of a dynamic number of rows using php and sql?
EDIT - Disclaimer: Noob at PHP and SQL编辑 - 免责声明:PHP 和 SQL 的菜鸟
I need to subtract a different value from each amount row based on the vegetable that is selected.我需要根据所选蔬菜从每个金额行中减去不同的值。
For example: If the user selects tomatoes and capsicum, I would need to subtract 200 and 400 from each of those amounts respectively.例如:如果用户选择西红柿和辣椒,我需要分别从这些数量中减去 200 和 400。
Is there some way to attach these values to the vegetable?有没有办法将这些值附加到蔬菜上? I have an array that looks like this:我有一个看起来像这样的数组:
**Array ( [0] => tomatoes [1] => onions [2] => spinach ** **数组([0] => 西红柿 [1] => 洋葱 [2] => 菠菜 **
ingredientsTable +----+-------------+---------+--------+-------+ | id | name | type | amount | unit | +----+-------------+---------+--------+-------+ | 1 | tomatoes | veggies | 1000 | grams | | 2 | onions | veggies | 1000 | grams | | 3 | spinach | veggies | 1000 | grams | | 4 | capsicum | veggies | 1000 | grams | | 5 | basil | veggies | 1000 | grams | +----+-------------+---------+--------+-------+
配料表+----+-------------+---------+--------+-------+ | id | name | type | amount | unit | +----+-------------+---------+--------+-------+ | 1 | tomatoes | veggies | 1000 | grams | | 2 | onions | veggies | 1000 | grams | | 3 | spinach | veggies | 1000 | grams | | 4 | capsicum | veggies | 1000 | grams | | 5 | basil | veggies | 1000 | grams | +----+-------------+---------+--------+-------+
+----+-------------+---------+--------+-------+ | id | name | type | amount | unit | +----+-------------+---------+--------+-------+ | 1 | tomatoes | veggies | 1000 | grams | | 2 | onions | veggies | 1000 | grams | | 3 | spinach | veggies | 1000 | grams | | 4 | capsicum | veggies | 1000 | grams | | 5 | basil | veggies | 1000 | grams | +----+-------------+---------+--------+-------+
I have an array of vegetables in PHP.我在 PHP 中有一系列蔬菜。 I am thinking of iterating over that array and updating the column based on that vegetable.我正在考虑迭代该数组并根据该蔬菜更新该列。 So possibly:所以可能:
foreach($vegetable as $subject)
($query = $conn->prepare("UPDATE ingredients set amount = amount - ? where `name` = '$item';"))
$query->bind_param("i", $amount);
$query->execute();
$insertresult = $query->get_result();
`
The issue is, I don't know how to change the $amount and I know this code does not subtract values at all.问题是,我不知道如何更改 $amount 并且我知道这段代码根本不会减去值。
At the moment, I've hardcoded $amount as 200, but I don't want to just update the amount for each row to 200. I need it to somehow subtract 200 from whatever the current value is - in this case it is 1000, so 1000 - 200, and update the value to 800. And for capsicum, I would need to subtract 400.目前,我已将 $amount 硬编码为 200,但我不想只将每行的金额更新为 200。我需要它以某种方式从当前值中减去 200 - 在本例中为 1000 ,所以 1000 - 200,并将值更新为 800。对于辣椒,我需要减去 400。
I've looked at Update multiple rows in 1 column in MySQL but that does appears not to be dynamic.我查看了在 MySQL 的 1 列中更新多行,但这似乎不是动态的。 It assumes we know in advance which rows to change and what the final value will be.它假设我们事先知道要更改哪些行以及最终值是什么。
EDIT: I just checked Mysql query: decrease value by 1 which I think solves the problem of subtracting values, but it doesn't explain how to link those values to the vegetable in PHP.编辑:我刚刚检查了Mysql 查询:将值减 1 ,我认为这解决了减去值的问题,但它没有解释如何将这些值链接到 PHP 中的蔬菜。 Example: How do I get and link the 200 amount to tomatoes, and 400 to capsicum?示例:如何将 200 的数量与西红柿和 400 与辣椒的数量联系起来?
EDIT 2: Updated code to subtract value and corrected syntax, clarified question.编辑 2:更新代码以减去值和更正语法,澄清问题。
The answer was to modify the way I was getting the data.答案是修改我获取数据的方式。 So from this:所以从这里:
<input type="checkbox" name="topping[]" id="toppingCheckbox" value="tomatoes">
I needed to instead change the name and value attributes:我需要更改名称和值属性:
<input type="checkbox" name="topping[tomatoes]" id="toppingCheckbox" value="200">
The subtraction was a simple sql query, answered here.减法是一个简单的 sql 查询,在这里回答。 Mysql query: decrease value by 1 Mysql查询:减1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.