file sends to folder but does not send data to database

Asked

Viewed 26 times

-1

I skipped a site to a server, making all the necessary settings, I have gallery control that has an upload file that sends the data to the folder and the information to the bank, nothing out of the ordinary, but the problem is that the way I did, just sending the data to the folder and not to the bank... What I’m doing wrong?

Follow the code block I’ve tried so far:

<?php
if(!empty($_FILES)){
require 'conn/conn1.php';
 $targetDir = "uploads/";
    $fileName = $_FILES['file']['name'];
    $targetFilePath = $targetDir.$fileName;

 if(move_uploaded_file($_FILES['file']['tmp_name'], $targetFilePath)){
        $insert = $conn1->query("INSERT INTO files (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");
    }
}
?> 
  • What remains is to treat the error that is possibly happening on INSERT, and for that there are N forms. If your connection is in PDO, you can already extract some information by placing this small stretch after the INSERT: var_dump($conn1->errorInfo());.

  • thank you, I discovered the problem, it was in the table structure in the bank, but thank you

1 answer

0


Daniel, an alternative is you generate a unique name for the file, and in the database just enter the file name. Thus, when using for example <img src="caminho/nomedafoto"> you get to rescue her. It would be something like:

$nome_imagem = md5(uniqid(time())) . ".jpg" ; //extensão de exemplo
$caminho_imagem = "uploads/" . $nome_imagem;
move_uploaded_file($imagem["tmp_name"], $caminho_imagem);
$insert = $conn1->query("INSERT INTO files (file_name, uploaded_on) VALUES ('".$nome_imagem."', NOW())");

Browser other questions tagged

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