Insert, upload and Update in the same process

Asked

Viewed 87 times

1

Good to have a problem here that I have tried to solve in several ways without success. It follows part that is catching

$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, just not uploading the image or updating the image in the comic

some solution?

  • 1

    If each news has a unique image, why not save the file named with the news id, or even inside the folder you created using the id, but with a default name?

1 answer

3


The problem is here:

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

Look at the quotes.

Either you switch to double quotes or concatenate:

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

You have to put quotes on query and also concatenate:

$sqlInsert2 = 'UPDATE noticias SET imagem = "' . $filename . '" WHERE idnoticia = "' . $lastId . '"';
  • I will make the change here and qq thing return

  • Perfect Lucas, I’m still crawling in this business of PDO, only the title of doubting has to do this process in a single time? type I’m wanting to get the next ID before I even insert the matter to create the folder. it’s possible or this method I’m doing is the right one?

  • 1

    Great. I don’t know if you can get the next ID, but even if you do, I don’t think it’s good practice because someone can enter a record after they get the next ID and before they enter it into the table

  • My idea was to make 1 INSERT only instead of INSERT and UPDATE. Before Insert he took the next id, created the folder, uploaded and Insert at once, more anyway ta working and once again thank you

  • 2

    I usually use transaction (http://php.net/manual/en/pdo.begintransaction.php) to avoid inconsistencies in the database

  • 1

    @Arsomnolasco Why don’t you get the file name before Insert?

  • Like I said, I’m still crawling, plus what’s the catch and disadvantage before or after? @adrianosymphony $dbh->beginTransaction(); $dbh->rollback(); Really no risk of cucumber

  • 1

    @Arsomnolasco in your case can catch before, you do not do any validation if the directory was created and the file moved or not, but should do such. Ps: when directing the response to someone check the same, so a notification will appear in the Inbox.

  • thanks for the tips and I will follow the advice

Show 4 more comments

Browser other questions tagged

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