【发布时间】:2015-09-24 00:04:17
【问题描述】:
查询返回按运算符分组的元素总和(由复选框选中)。
如何总结所有这些值?我尝试使用 array_sum() 但没有奏效,或者我没有正确使用这个函数。 谢谢
<?php
if(isset($_POST['delete']))
{
$ziua=$_POST["date"];
}
else
{
$ziua=date('Y-m-d');
}
if(isset($_POST['delete']))
{//check to see if the delete button has been pressed
if(isset($_POST['box']))
{ //check to see if any boxes have been checked
$num = 0;//used to count the number of rows that were deleted
$box = $_POST['box'];
foreach ($box as $key =>$val)
{ //loop through all the checkboxes
$num++;
$sqldel=" SELECT U.username , SUM(L.geometrie1) A from list L,users U where L.user_id='$val'
and L.date_posted like '%$ziua%' AND L.user_id=U.id group by U.username ";//delete any that match id
$resdel=mysql_query($sqldel);//send the query to mysql
while($row = mysql_fetch_array($resdel))
{
Print "<tr>";
Print '<td align="center">'. $row['0']. "</td>";
Print '<td align="center">'. $row['1']. "</td>";
Print "</tr>";
}
}
}
}
?>
<!-- end snippet -->
【问题讨论】:
-
结果类似于:admin -> 5, user 8 我想成为 admin -> 5, user 8, total-> 13
-
一如既往:请停止使用已弃用
mysql扩展。 read the red warning box: 扩展不再维护,以后会被移除。学习使用PDO或mysqli(i用于改进)。还要了解准备好的陈述,当你在做的时候。PDO更常用(因为它的 API 更好)。mysqli提供了一个过程 API 和一个面向对象的 API,但是更加混乱。他们都有自己的强项和弱点,所以两个都玩,看你更喜欢哪一个 -
你很容易受到sql injection attacks
-
请发布完整代码。我没有看到你要添加的任何地方
-
@NanaPartykar:这就是他首先发布问题的原因:OP 没有在循环中添加任何内容,这就是问题所在:P
标签: php checkbox foreach while-loop sum