简体   繁体   English

与数据库交互的提交输入类型

[英]a submit input type that interactes with database

how can I with a input type submit can increment something or decrement something in a database?我如何使用输入类型提交可以增加或减少数据库中的某些内容?

In my case I'm doing an inventory managment tool and in a html table I have a input type that adds stock and other that removes, how can I connect those buttons with the database?在我的情况下,我正在做一个库存管理工具,在 html 表中,我有一个添加库存和其他删除的输入类型,如何将这些按钮与数据库连接?

In the form, use two inputs with the same name but different values.在表单中,使用名称相同但值不同的两个输入。

<input type="submit" name="direction" value="+">
<input type="submit" name="direction" value="-">

Then in the PHP, check the value of $_POST['direction'] and use that in the database update.然后在 PHP 中,检查$_POST['direction']的值并在数据库更新中使用它。

if ($_POST['direction'] == '+') {
    $increment = 1;
} elseif ($_POST['direction'] == '-') {
    $increment = -1;
} else {
    die("Invalid direction");
}
$stmt = $conn->prepare("UPDATE yourTable SET something = something + ? WHERE id = ?")
$stmt->bind_param("ii", $increment, $id);
$stmt->execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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