Insert, upload and Update in the same process

2

Well with a problem here that I tried to solve in several ways without success. Here's the catching part.

$stmt->execute();  -------> final do insert até aki ok
if (DB::lastInsertId()) {
// pego o ultimo id
$lastId = DB::lastInsertId();
// crio o diretorio
mkdir ("../img/$lastId", 0755 ); ---> ate aqui ok o diretorio e criado usando o ultimo id
---------------------> a partir daqui que esta o problema
// upload e update
// altero o nome
$filename = time() . '_' . $_FILES["imagem"]["name"];
$diretorio = '../img/$lastId/';
$filepath = '$diretorio' . $filename;
move_uploaded_file($_FILES["imagem"]["tmp_name"], $filepath)
// insere no bd
$sqlInsert2 = "UPDATE noticias SET imagem=$filename WHERE idnoticia=$lastId";
$stmt = DB::prepare($sqlInsert2);
$stmt->bindParam("imagem", $filename);
$stmt->execute();


 setMessage("Notícia $lastId $filename cadastrado com sucesso.");
    redirect("noticiasListar.php");

The other news items are being inserted, the folder is being created, you are not uploading the image and the image update in bd

Any solution?

    
asked by anonymous 26.02.2015 / 21:00

1 answer

3

The problem is here:

$diretorio = '../img/$lastId/';
$filepath = '$diretorio' . $filename;

Notice the quotes.

Or you switch to double quotation marks or concatenate:

$diretorio = '../img/' . $lastId . '/';
$filepath = $diretorio . $filename;

You have to put quotation marks around query and concatenate too:

$sqlInsert2 = 'UPDATE noticias SET imagem = "' . $filename . '" WHERE idnoticia = "' . $lastId . '"';
    
26.02.2015 / 21:04