Copy id with 2 Insert

Asked

Viewed 27 times

-1

Well I have two INSERT in the same file, and I want the id of the first INSERT(Tabela1) also go to my second INSERT(table2), I really do not know how to do, I will leave everything more detailed below.

INSERT 1 Advertisement

$query = "INSERT INTO criar_anuncio (nome_funcionario, tipo_anuncio, cidade, cep, endereco, numero_casa, bairro, visibilidade, telefone, wpp, id_user_anun, data_cadastro) VALUES ('$post_funcionario', '$tipo_anuncio', '$cidade', '$cep', '$endereco', '$num_casa', '$bairro', '$visibilidade', '$telefone', '$wpp', '$id_user_anun', NOW())";

Until then a normal INSERT. Now I wanted the id that is auto increment to go to my second table. INSERT 2 Photos of the rooms

$query_comodos = "INSERT INTO `img_comodos`( `id_user`, `id_anuncio`, `img_file`) VALUES ('$id_user_anun', '$img_final_comodo')";

Well, I’m trying to get my table id Announcement to my table Photos Comfortable, with each id occupying the field id_anuncio on my table 2.

Here’s my first table inserir a descrição da imagem aqui

Here is my second table, and I want to occupy the field id_anuncio inserir a descrição da imagem aqui

  • Take a look at last insert id, I think that’s what you need

1 answer

0


Hello! You can recover the Insert ID using the function insert_id

$query_comodos = "INSERT INTO `img_comodos`( `id_user`, `id_anuncio`, `img_file`) 
 VALUES ('$id_user_anun', '$img_final_comodo')";

$mysqli->query($query_comodos);
$id_anuncio = $mysqli->insert_id;

Browser other questions tagged

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