Insert into foreign keyed tables with PDO

0

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

Hello everyone, I'm good with a doubt, I have a relationship between two tables in my mysql database as follows:

MyquestionishowcanIgettheinsertioninthetwotablessinceinmywireframeasrequirementisrequestedthis,thenmyformIhavethefollowing:

<formaction="#" 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 image_id at that point in my code:

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

where you read the number 42 would be where I would like to pass my image_id in this case I passed it without being dynamic to test, the process is the following I enter my image and then I want to insert my image description just for that image

If anyone has a suggestion thank you, I'm using PDO.

    
asked by anonymous 02.05.2015 / 02:13

1 answer

0

In the insert method of the image, return the inserted record id with lastInsertId , save 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);     
    
02.05.2015 / 02:43