gpt4 book ai didi

php sql 在选中复选框时获取查询

转载 作者:行者123 更新时间:2023-11-29 10:48:43 24 4
gpt4 key购买 nike

我只是想知道如何在 SQL 中获取列的值,当选中该复选框时,它会将这些值相加,因为它们是数字。

数据

表名=添加

    SEARCH = Dan

-----------------------
name | date | Total | Checkbox
-------------------------------------
Dan | dd/mm/yyy | 200 | checked
Dan | dd/mm/yyy | 400 | checked
Dan | dd/mm/yyy | 300 | checked
Dan | dd/mm/yyy | 250 | unchecked
Dan | dd/mm/yyy | 260 | unchecked

TOTAL AMOUNT = 900

更新:这是我到目前为止所做的代码,但它仍然无法工作。

 <?php  

include "connection.php";

//$searchName= $_POST['submit'];


if(isset($_POST['submit'])){
$searchName = $_POST['submit'];

$searchName = preg_replace("#[^0-9a-z]#i","",$searchName);

$query = "SELECT * FROM adding where Code LIKE '$searchName%' ORDER BY Date";
$result = mysql_query($query);

if(mysql_num_rows($result)>0){
echo "<form action=Payment.php method=get>";
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Date</th>
<th>Total</th>
<th>Checkbox</th>
</tr>";
while($row = mysql_fetch_array($result)){
//$idno = $row['id'];

echo "<tr>";
echo "<td>" . $row['Code'] . " </td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Total'] . "</td>";
echo "<td><input type = \"checkbox\" name=\"pay[]\" value='{$row['id']}' ></td>";
echo "</form>";
}
echo "<table>";
echo "<input type=\"submit\" name =\"button2\" value=\"Submit\">";
echo "</form>";

if(isset($_GET['pay'])){

$checked=$_GET("pay");
$c= count($checked);
$totalpayment = $row['Total'];

for($i=0;$i<$c;$i++){
if($checked[$i]==$row['id']){
$totalpayment = $totalpayment+$row['Total'];
}
}
echo "total is: " .$totalpayment. "<br>";

}


}
else{
echo "RECORD NOT FOUND!";
}




}

mysql_free_result($result);


?>

最佳答案

试试这个

select sum(total) as TOTAL AMOUNT from table
where Checkbox='checked'

关于php sql 在选中复选框时获取查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44129418/

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