Why in 'due column', same value is showing for every clients?

I have become able to display due data in due column but the same value is showing for each customer - what is the wrong I am doing? Here is the code, please somebody help!

while ($row_custact = mysqli_fetch_assoc($query_custact)){
				$currentuser = $row_custact['cust_id']; 
				$sql_inccur ="SELECT i.inc_date, t.inctype_type, i.inc_amount, i.inc_text, c.cust_no, c.cust_name, i.inc_receipt
                From customer AS c
                LEFT JOIN incomes AS i ON c.cust_id = i.cust_id 
                LEFT JOIN inctype AS t ON i.inctype_id = t.inctype_id
                WHERE c.cust_id = '$currentuser'
                ORDER BY inc_date DESC";

                $query_inccur = mysqli_query($db_link, $sql_inccur);
	            checkSQL($db_link, $query_inccur);      
				
				
					
				while ($row_inccur = mysqli_fetch_assoc($query_inccur)){
					
				$inc_amount = $row_inccur['inc_amount'];
				$inc_text = $row_inccur['inc_text'];	
				
				
				//Iterate over income types and add matching incomes to $total
					 
						$total_row = $total_row + $row_inccur['inc_amount'];  
						$total_paid = $total_paid + $total_row;
				
				// part for total due finding.. and "inc_text" is due column
				        $total_row_due = $total_row_due + $row_inccur['inc_text'];  
				        $total_due = $total_due + $total_row_due;
				
				// this part gathers only total due paied for an account
				if($row_inccur['inctype_type']=='Duepay')
				{
					$total_duepay = $total_duepay + $row_inccur['inc_amount']; 
				}
				$remaining_due = $total_row_due - $total_duepay;	
				//echo $remaining_due; 	
					
				}
				
				
				//echo $currentuser; 
					
				echo '<tr>
								<td>
									<a href="customer.php?cust='.$row_custact['cust_id'].'">'.$row_custact['cust_no'].'</a>
								</td>
								<td>'.$row_custact['cust_name'].'</td>
								<td>'.$row_custact['custsex_name'].'</td>
								<td>
								    '.$remaining_due. '  // showing due left, here is the problem 
								</td>
								
								<td>'.$row_custact['cust_address'].'</td>
								<td>'.$row_custact['cust_phone'].'</td>
								<td>'.date("d.m.Y",$row_custact['cust_since']).'</td>
							</tr>';
				}
  1. Do not run queries in a loop
  2. Do not ever put variables in a query. Use Prepared Statements.

@ benanamen
thank you a lot. I will follow this.

I would also highly recommend you use PDO. Here is a tutorial to get you going.
https://phpdelusions.net/pdo

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service