Whenever you need to make a relation, in the table that has the data that will relate to the user (in your case), you should have a column identifying the user ID, so you can make the relation. Then change the structure of tbl_comissao
for:
ID | id_usuario | comissao
1 | 1 | 13
2 | 2 | 26
To relate you can make 2 querys. One to pull the commissions and in the loop do another query to see which is the user of id_usuario
$result = mysqli_query($con,"SELECT id, comissao FROM tbl_comissao");
echo "<table border='1'>
<tr>
<th>Representante</th>
<th>Comissão</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$usuarioQuery = mysqli_query($con,"SELECT nome FROM tbl_usuario WHERE id = '".$row['id_usuario']."' ");
$rowUser = mysqli_fetch_array($usuarioQuery);
$id = $row['id'];
$comissao = $row['comissao'];
echo "<tr>";
echo "<td>" . $rowUser[0]['nome'] ."</td>";
echo "<td>" . "R$ ".number_format($comissao,2, ',', '.') . "</td>";
echo "</tr>";
}
echo "</table>";
But the best solution is to use INNER JOIN
or LEFT JOIN
or RIGHT JOIN
(depends on your need) to make the relationship:
$result = mysqli_query($con,"SELECT c.id, c.comissao, u.nome FROM tbl_comissao AS c INNER JOIN tbl_usuario AS u ON u.id = c.id_usuario");
echo "<table border='1'>
<tr>
<th>Representante</th>
<th>Comissão</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$comissao = $row['comissao'];
echo "<tr>";
echo "<td>" . $row['nome'] ."</td>";
echo "<td>" . "R$ ".number_format($comissao,2, ',', '.') . "</td>";
echo "</tr>";
}
echo "</table>";
Read this topic to see how to use the Joins clauses
I cannot rename, because there are other places since they query this table by name id
– Leandro Teraoka
You can pull the "id" field, only change the SQL "id_usuario" to "id" in the code I posted ;) ... But it would be better to create the way you said, because it gets better and runs less risk of trouble, but depends on your need.
– Alisson Acioli