Merge two tables with PHP and recover the data in the same html select element

Asked

Viewed 224 times

-1

I’m trying to list in the same element select of html the results of the consultation of different tables, but I’m not getting.
Follow down the code so someone can tell me where I’m going wrong.

<div class="form-group">
    <label>Selecione a turma:</label>
    <select name="id_disciplina_turma_facul">
        <?php
            $consulta = \MySql::conectar()->prepare("SELECT tb_turma_facul.id_turma_facul as turma,
                                                 tb_turma_facul.id_disciplina_turma_facul,

                                                tb_disciplina_facul.id_discip_facul,
                                                tb_disciplina_facul.nome_discip_facul as dnome,                                                   
                                                 tb_turma_facul.id_professor_turma_facul,
                                                 tb_professor_facul.nome_professor as pnome

                                                   FROM tb_turma_facul

                                          INNER JOIN (tb_disciplina_facul, tb_professor_facul)

                                          ON (tb_turma_facul.id_disciplina_turma_facul =
                                          tb_disciplina_facul.id_discip_facul 

                                          AND tb_turma_facul.id_professor_turma_facul =
                                          tb_professor_facul.cpf_professor)");


            $consulta->execute();
            $consulta = $consulta->fetchAll();
            foreach ($consulta as $key => $value) {
        ?>

            <option value="<?php echo $value['dnome'] ?>">
                <?php echo $value['pnome']; ?>
            </option>

        <?php } ?>

    </select>
</div><!--form-group-->

<div class="form-group">
    <input type="submit" name="acao" value="Matricular em turma">
</div><!--form-group-->

Table columns are described below.
Being, that the table turma owns the foreign keys of the tables disciplina and professor.

Table columns tb_disciplina_facul:

  • id_discip_facul
  • nome_discip_facul
  • carga_horaria_discip_facul

Table columns tb_professor_facul:

  • nome_professor
  • endereco_professor
  • complemento_professor
  • cep_professor
  • bairro_professor
  • cidade_professor
  • estado_professor
  • telefone_professor
  • formacao_professor
  • titulacao_professor

Table columns tb_turma_facul:

  • id_turma_
  • id_disciplina_turma_facul
  • id_professor_turma_facul
  • How many tables are there, friend? I can make a basic example and Oce adapts in how many cases it is best

  • Note that the columns of each table are described below the code, so you can base yourself to look similar to mine.

  • There are 3 tables, the first of the subjects, the second of the teachers and the third is called class. The latter will possess the foreign keys of the first two.

1 answer

0

If the columns described are true to what is in your database, you have two typos in your select: id_turma_ is as id_turma_facul and id_discip_facul is as id_disciplina_facul.

I made these adaptations and got the desired result.

SELECT 
  tb_turma_facul.id_turma_facul as turma,
  tb_turma_facul.id_disciplina_turma_facul,
  tb_disciplina_facul.id_discip_facul,
  tb_disciplina_facul.nome_discip_facul as dnome,                                                   
  tb_turma_facul.id_professor_turma_facul,
  tb_professor_facul.nome_professor as pnome
FROM tb_turma_facul
   INNER JOIN (tb_disciplina_facul, tb_professor_facul)
ON (tb_turma_facul.id_disciplina_turma_facul = tb_disciplina_facul.id_discip_facul 
AND tb_turma_facul.id_professor_turma_facul = tb_professor_facul.cpf_professor)

inserir a descrição da imagem aqui

PHP was also successful in the result.

inserir a descrição da imagem aqui

  • Solved the problem, I changed only id_disciplina_facul to id_discip_facul. Regarding another column, the name is id_turma_facul even, because I put the incomplete name.

Browser other questions tagged

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