Insert into foreign key tables with PDO

Asked

Viewed 553 times

1

How to insert into tables with one to many relationship containing foreign key

Hello everyone all right I’m having a doubt, I have a relationship between two tables in my mysql data ban as follows: inserir a descrição da imagem aqui

My question is how I will be able to insert the two tables since my wireframe as a requirement is requested this, so my form I have the following:

<form action="#" method="POST" enctype="multipart/form-data">
            <h2>Painel de inserção de imagens</h2>
            <input class="btn btn-success" required="" type="file" name="fileUpload" id="passaValor" title="inserir imagens"> <br></br>
            <label>Qual Pagina deverá ser exibida</label>


            <select required="" name="id_pagina">
                
                <?php
                    $daoPagina->selecionaTodasPaginas();
                ?>
            </select>
            

            <label>Titulo, Coloque até 50 letras</label>
            <input class="input-xxlarge" type="text" name="titulo" required=""> <br/><br/> 
            <label>Descrição total das imágens não poupe suas palavas</label>
            <textarea class="input-xxlarge" name="textoDaImagem" required="" rows="5"> </textarea><br></br>


            <input class="btn btn-large btn-primary" type="submit" value="Enviar">
        </form>

Well the question of inserting images is succinct for me, what I would like to know is, how do I pass the id_image at this point of my code:

$daoDescricaoImagem = new DescricaoImagemEntity("", $titulo, $textoDaImagem, 42);

where if reading the number 42 would be where I would like to pass my id_image in this case passed it without being dynamic to test, the process is the following I insert my image and then I want to insert my image description just for this image.

if anyone has a suggestion thank you, I am using PDO.

  • The information is in different form?

  • are not in the same form.

1 answer

1


In the image Insert method return the id of the record inserted with lastInsertId, store this value in a variable or object and pass it to DescricaoImagemEntity.

DAO image

public function insertImagem($imagem){
   //código do insert
   $smtm->execute(); 
   return $this->connection->lastInsertId(); 
}

File that saves information:

<?php
 $daoImagem = new DAOImagem();

 //$imagem é uma objeto populado com as informas de $_POST
 $id_imagem = $daoImagem->insertImagem($imagem); 
 $daoDescricaoImagem = new DescricaoImagemEntity("", $titulo, $textoDaImagem, $id_imagem);     
  • always you answering my questions, this time had passed close to correct the answer. had put Return $smtm->lastInsertId(); thanks.

  • @Andrémartins, is using mysql or postgres?

  • mysql, postgres use more with java

  • Why the doubt? @rray

  • @Andrémartins, postgres has something nice that is good to know that it is classic returning which returns the id already entered in the Insert then does not need to call lastInsertId but mysql doesn’t have this :P

  • that mass... adding this né RETURNING meu_id, my English is weak is why it had not worked...

  • when I was reading the documentation I didn’t notice :(

Show 2 more comments

Browser other questions tagged

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