I believe that the issue is not the code itself, so I will focus only on logic.
When you go through the image variable with the foreach, you compare their position with the variable $key. Then in the if that compares to $key == 0, goes the code that makes the insertion in the database and moves to the folder the first image, and in the else, will do the same with the second image
foreach($_FILES['imagem'] as $key => $imagem) {
if($key == 0) {
// Codigo que vai salvar na coluna imagem_1 e mover para a pasta
} else {
// Codigo que vai salvar na coluna imagem_2 e mover para a pasta
}
}
That answer might help you: How to treat upload muilt the right way
– DNick