我创建了一个表格,其中只创建了一个<tr>
和11 <td>
。前9个<td>
的值来自一个while循环,最后2个<td>
值来自第二个while循环。
While
循环正在<tr>
中执行。现在,我得到了正确的值,但它重复了<td>
作为记录的总数,例如我总共有5条记录,它给了我25条记录,第一条记录连续执行了5次并且像那样休息。
我的问题是执行一次记录,而不是重复记录。
这是我的代码:
<table id="myTable" class="table table-bordered table-hover">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Status</th>
<th>Reg on</th>
<th>Upload</th>
<th>Total Upload</th>
<th>Sale</th>
<th>Total Sale</th>
<th>Purchase</th>
<th>Total Purchase</th>
</tr>
</thead>
<tbody>
<?php
include("connection.php");
$query ="SELECT s.*, s.student_id, s.student_email, u.student_email, b.payer_email,
COUNT(u.student_email) AS 'uploadCount',
SUM(u.price) AS 'uploadTotal',
COUNT(b.payer_email) AS 'buyCount',
SUM(b.payment_amount) AS 'buyTotal'
FROM students s
LEFT JOIN academic_work u ON u.student_email = s.student_email
LEFT JOIN orders b ON b.payer_email = s.student_email
GROUP BY s.student_id
ORDER BY s.student_id DESC
";
$run = mysqli_query($con, $query) or die(mysqli_error($con));
?>
<tr>
<?php
while ($row=mysqli_fetch_array($run)) {
$student_id=$row['0'];
$student_first_name=$row[1];
$student_last_name=$row[2];
$student_email=$row[3];
$student_password=$row[4];
$student_status=$row[5];
$student_time=$row[6];
$uploadCount = $row['uploadCount'];
$buyCount = $row['buyCount'];
$uploadTotal = $row['uploadTotal'];
$buyTotal = $row['buyTotal'];
$query1 ="SELECT ss.*, ss.student_id, ss.student_email, p.email,
COUNT(p.email) AS 'purchaseCount',
SUM(p.payment_amount) AS 'purchaseTotal'
FROM students ss
LEFT JOIN orders p ON p.email = ss.student_email
GROUP BY ss.student_id
";
$run1 = mysqli_query($con, $query1) or die(mysqli_error($con));
while ($row1=mysqli_fetch_array($run1)) {
$purchaseCount = $row1['purchaseCount'];
$purchaseTotal = $row1['purchaseTotal'];
?>
<td><?php echo $student_first_name; ?></td>
<td><?php echo $student_last_name; ?></td>
<td><a href="academic_work_add.php?add_academic_work=<?php echo $student_id; ?>" style="text-decoration:none;"><?php echo $student_email; ?></a></td>
<td><?php echo $student_status; ?></td>
<td><?php echo $student_time; ?></td>
<td><?php if($uploadCount == 0) { echo 0; } else { echo $uploadCount; }?></td>
<td><?php if($uploadTotal == 0) { echo 0; } else { echo $uploadTotal; }?></td>
<td><?php if($buyCount == 0) { echo 0; } else { echo $buyCount; }?></td>
<td><?php if($buyTotal == 0) { echo 0; } else { echo $buyTotal; }?></td>
<td><?php if($purchaseCount == 0) { echo 0; } else { echo $purchaseCount; }?></td>
<td><?php if($purchaseTotal == 0) { echo 0; } else { echo $purchaseTotal; }?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
提前致谢!
答案 0 :(得分:0)
您的$query1
在每次迭代时循环遍历表的所有行。使用where
子句然后它将返回您想要的1行。使用$row
的ID定位正确的行。您应该对此进行参数化,这样您就不会打开SQL注入(https://security.stackexchange.com/questions/146595/second-order-sql-injection-protection)。
这也可以在1个查询中完成,循环查询通常是不正确的实现。