INNER JOIN with Where

Asked

Viewed 160 times

2

I have the following tables : permissions_users

  • id_user
  • id_permissao

permissions - id - permission

I have a session variable where I have the id of user , I need to do a table search for id and those that have the id of the user display the name of the permission... How can I do?

Error testing the first answer:

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projetoFinalBD/permissao.php on line 51

Complete code :

<body>
<?php
$id_user = $_GET['id_user']; 
function user_info_gamb ($id_user) {
     $sql = mysql_query("SELECT * FROM usuarios WHERE id_user = '$id_user'");
      return mysql_fetch_assoc($sql);
}
$nome = user_info_gamb($id_user);
//$permissao_pg = 1; include('funcoes/restringirpg.php'); 
?>
<div id="topo">
<?php  echo "Bem - Vindo  " . $nome["nome"]; include('funcoes/menu.php');?>
</div>

<div id="conteudo">
<table class="table-auto table-striped text-center" width="auto" border="0" align="center" >
  <tr>
    <td><strong>PERMISSÕES</strong></td>
  </tr>

  <?php
  function permissao_info ($id_permissao) {
      $sql = mysql_query("SELECT * FROM permissoes WHERE id = '$id_permissao'");
      return mysql_fetch_assoc($sql);
  }
    $aux = 0;

      $permissoes = mysql_query(" SELECT PU.ID_USER, P.PERMISSAO FROM PERMISSOES_USERS PU INNER JOIN PERMISSOES P ON PU.ID_PERMISSAO = P.ID ");
        for ($i=0; $row=mysql_fetch_assoc($permissoes); $i++){$permissao = $row["permissao"];

            echo "
            <tr>
                <td>$permissao</td>
            </tr>
            ";

        }

  ?>

</table>

1 answer

5


Try this

SELECT PU.ID_USER, P.PERMISSAO FROM PERMISSOES_USERS PU
INNER JOIN PERMISSOES P ON PU.ID_PERMISSAO = P.ID
WHERE PU.ID_USER = 1

About the error

Notice: Undefined index: permissao in /Applications/XAMPP/xamppfiles/htdocs/DEV WEB/projectFinalBD/permissao.php on line 51

is because here $permissao = $row["permissao"] you’re trying to access the column permissao but the column name is PERMISSAO (uppercase).

  • went wrong ....

  • 5

    Saying "went wrong" is not enough right. What went wrong?

  • I just added the error message to the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.