使用复选框从数据库汇总列

时间:2017-03-29 20:01:29

标签: php html mysql checkbox mysqli

我是php的新手。我在两列数据库中只插入了一个值。我正在尝试使用复选框添加它们。 例如。有一个名为“englishprice”的列只有一个值20,而另一列“mathprice”只有10个值。我想用复选框求和它们,如果我只检查英语价格然后显示它的价格,如果我同时检查两个然后它将两者相加。

1 个答案:

答案 0 :(得分:0)

试试这个>>

Html代码:
您需要创建两个带有值的复选框作为其价格。这是样本

<form method="post">
   <input type="checkbox" value="20" name="englishprice"/>englishprice
   <input type="checkbox" value="10" name="mathprice"/>mathprice
   <button type="submit" name="submit">Submit</button>
</form>

PHP代码:

<?php 
if (isset($_POST["submit"])) {
    $englishprice = isset($_POST['englishprice']) && !empty($_POST['englishprice']) ? (int) $_POST['englishprice'] : 0;
    $mathprice = isset($_POST['mathprice']) && !empty($_POST['mathprice']) ? (int) $_POST['mathprice'] : 0;
    echo "Price " . ($englishprice + $mathprice);
}
?>