-1

I'm trying to make a report to show how many task certain staff completed in a day. So the data I get is from 'task' table where each taskid has agentid. I don't know why last row keep showing this error, "Warning: Undefined array key 2 in C:\xampp\htdocs\tms\des_rptday.php on line 188". Look at the image for reference.

Result

So this is the code:

'''

  <?php 

    $no = 0;
    $sql5 = "SELECT count(*), status, agent FROM task WHERE dateupdate = '$tarikh4' AND status IN ('DELIVERED','ASSIGNED') GROUP BY agent";

         if($result5 = mysqli_query($conn, $sql5)){
            if(mysqli_num_rows($result5) > 0){
                while($row5 = mysqli_fetch_array($result5))
                {
                    $ajen5[] = $row5['agent']; 
                    $cntajen[] = $row5['count(*)'];
                    $no++;
                }
                mysqli_free_result($result5);
            }
        }
        
         print "<table width=50% border=1 align=left cellpadding=0 cellspacing=0 style=\"border-collapse: collapse;\">";
         print "<tr bgcolor=grey>";
         print "<td valign=top align=center>";
         print "NO</td>";
         print "<td valign=top align=center>";
         print "STAFF</td>";
         print "<td valign=top align=center>";
         print "TOTAL TASK DELIVERED</td>";
         print "</tr>";

         $nb = 1;
         foreach($ajen as $no1 => $ajen)
         {
          print "<tr>";
          print "<td valign=top align=center>";
          print "$nb.</td>";
          print "<td valign=top align=center>";
          print "$ajen5[$no1]</td>";
          print "<td valign=top align=center>";
          print "$cntajen[$no1]</td>";
          print "</td>";  
          print "</tr>";
          $nb++;
         }
         print "</table>";
         ?>
    </td></tr>

'''

DORA
  • 9
  • 3
  • 2
    Hey there! It seems that some code is missing. Where do you declare `$ajen`? – icydemon May 20 '22 at 10:20
  • check your foreach loop. it's better you var_dump() every variable before you add in HTML. also, attach the output in the question, so people will understand the flow clearly. – Gobinda Nandi May 20 '22 at 10:33
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 20 '22 at 11:15

0 Answers0