为什么我的数据在表中填充两次但它只在数据库中存储一次?

时间:2018-04-24 11:39:55

标签: php html sql loops while-loop

所以我的表格显示了两次相同的数据行。但是在数据库中它只存储一次。我认为while循环可能存在问题。我正在将数据插入到数据库中,该数据库在数据库中存储正常,但是当我将其打印到屏幕时,它会重复相同的值两次。

<?php
include ("dbConnect.php");

$query = $db->prepare("select * from tasks LEFT JOIN challenge ON tasks.task = challenge.challenge where start_date <= NOW() and end_date >= NOW() ORDER BY challenge.achievement DESC limit 8");
$query->execute();
// Display search result
         if (!$query->rowCount() == 0) {

                echo "<table id=\"example\" style=\"font-family:arial;color:#ffffff;\" class=\"table table-striped table-bordered\" style=\"width:100%\">"; 
                echo "<thead><tr>
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Customer ID</th>
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Name</th>                
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Score</th>
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Challenge</th>
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Rank</th>
            </tr></thead><tbody>";
            $position = 1; 
            while ($results = $query->fetch()) {


                $count = count($results);

                echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";            
                echo $results['cust_id'];
                echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";
                echo $results['forename'];
                echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";
                echo $results['achievement'];
                echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";
                echo $results['challenge'];
                echo "<td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";
                echo "Position: $position";
                $position = $position+1;

                } 
}       

                echo '</td></tr></tbody>';              

                echo "</table>";            

?>

3 个答案:

答案 0 :(得分:1)

看起来你错过了你的循环中的结束td和tr。但是在使用LEFT JOIN的查询中,每次挑战将获得尽可能多的任务。因此,如果任务1有2个挑战,那么它将显示任务1,2次。每次挑战1次。希望这可以帮助。 下面我把你的TD TR关闭到了循环中。

 <?php
include ("dbConnect.php");

$query = $db->prepare("select * from tasks LEFT JOIN challenge ON tasks.task = challenge.challenge where start_date <= NOW() and end_date >= NOW() ORDER BY challenge.achievement DESC limit 8");
$query->execute();
// Display search result
         if (!$query->rowCount() == 0) {

                echo "<table id=\"example\" style=\"font-family:arial;color:#ffffff;\" class=\"table table-striped table-bordered\" style=\"width:100%\">"; 
                echo "<thead><tr>
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Customer ID</th>
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Name</th>                
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Score</th>
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Challenge</th>
                <th style=\"border-style:solid;border-width:3px;border-color:#575757;background:#575757;\">Rank</th>
            </tr></thead><tbody>";
            $position = 1; 
            while ($results = $query->fetch()) {


                $count = count($results);

                echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";            
                echo $results['cust_id'];
                echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";
                echo $results['forename'];
                echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";
                echo $results['achievement'];
                echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";
                echo $results['challenge'];
                echo "<td style=\"border-style:solid;border-width:1px;border-color:#D3D3D3;\">";
                echo "Position: $position";
                $position = $position+1;
                echo '</td></tr></tbody>';              
                } 
}       
            echo '</tbody>';              
            echo "</table>";            

?>

答案 1 :(得分:0)

任务LEFT JOIN挑战任务.task = challenge.challenge 您正在进行左连接,首先确认您的关系表中没有相同主键的多个条目

答案 2 :(得分:-2)

使用WebViewfetchAll()

foreach