Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to create array to calculate total by each group. But, this code was error in html table. This is the printout page.

What I have tried:

PHP
$ListEPF = array();
$mySQL = 'SELECT A.*, B.description AS grouping FROM payroll_monthly_kwsp_socso_eis_detail A INNER JOIN payroll_group B ON B.id = A.payroll_group WHERE A.company = '.$data['company'].' AND A.payroll_month = "'.$data['month'].'" ORDER BY A.payroll_group, A.id';
$result = mysql_query($mySQL); 
if ($result){
	if (mysql_num_rows($result) > 0){ 
		while ($row = mysql_fetch_assoc($result)){
          
          //TOTAL EACH GROUP
			//how to make array to calculate by group? 
			$mySQL2 = 'SELECT A.* FROM payroll_monthly_kwsp_socso_eis_group A WHERE A.company = '.$row['company'].' AND A.payroll_month = "'.$row['payroll_month'].'" ORDER BY A.payroll_group, A.id ';
			$result2 = mysql_query($mySQL2); 
			if ($result2){
				if (mysql_num_rows($result2) > 0){ 
					$row2 = mysql_fetch_assoc($result2);
					$ListEPF[$row['grouping']]['col_basic_salary_epf'] = $row2['basic_salary_epf'];

          if (!array_key_exists($row['id'], $ListEPF[$row['grouping']])){
				$ListEPF[$row['grouping']][$row['id']] = array();
			}
			$ListEPF[$row['grouping']][$row['id']]['basic_salary_epf'] = $row['basic_salary_epf'];
        }
    }
}




HTML
$i = 0;

foreach ($ListEPF as $key => $value){
    $html .= '';
    $html .= '
    '.$key.'
    Month: '.$data['month'].' 
    Month: '.($data['month'] - 1).'
    
    Month:
    ';
    $html .= 'Salary';
    $html .= '';
    $html .= '';
    foreach ($value as $key2 => $value2){
        $i++;
        $html .= '';
        $html .= ''.$value2['basic_salary_epf'].'';
        $html .= '';
        
    }

    //TOTAL
    $html .= '';
    $html .= ''.$value['col_basic_salary_epf'].''; //Total All Basic Salary by group
    $html .= ''; 
    $html .= '';
    
    $html .= '  ';
}
Posted
Updated 8-Feb-23 18:48pm

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900