PHP: Upload an image to a database

3

At this moment I am working on a project in PHP. I am basically new and I have many doubts, among which is the following:

How do I upload an image to a database using long_blob ?
Note that I have two buttons, one to send data to a table, and another to upload the image to another table.

Example of my code:

Evidencia

//Se escoge el archivo
<input type="file" name="Evidencia" value="Evidencia">

//Botón para enviar imagen
<input type="submit" name="SubirEvidencia" value = "Subir">

//Boton para subir datos(Irrelevante, pero vale la pena mencionarlo, ya que no estoy usando formulario.
<input type="submit" name="Enviar" value="Enviar">

if ($_POST['SubirEvidencia']) {
   $fecha= $_POST['Fecha'];
   $imagen = addslashes(file_get_contents($_FILES['Evidencia']['tmp_name']));
   $query= "INSERT INTO evidencia_dia(id_promotor, fecha, evidencia) 
   VALUES ('$idPromotor','$fecha', '$imagen')";
   $resultado = $conn->query($query);
?>
   <script type="text/javascript">
   alert("Se añadió imagen correctamente.");
   </script>
   <?php
}

The strange thing about this code is that it increases both the id_promotor and the date, but the file does not upload. Can you help me?

    
asked by Esteban Trevino Zwingil 06.08.2018 в 18:51
source

1 answer

1

The input name SubirEvidencia is not recognized in your code as a variable to be stored in your database, if you need to save the information from your form directly in the BD you have 2 options:

The first option

  

Do not forget to place your input inside the <form>

tags


You retrieve the content of your input in a variable with image name something like this:

$imagen = base64_encode($_REQUEST['SubirEvidencia']);

query should not give you errors

The second option
You can use the tool summernote Available here which is a JavaScript library and will allow you to make your input have an embedding tool images in their respective content, and if you need to store the image in a separate column you can perfectly do it from your PHP extracting the content that will be encoded in base64.

  

Take into account that in your database you must configure it as long_blob since it is the one that will allow you to accept the long text strings that are generated from the base64 encoding



I hope my answer is useful.

    
answered by 30.11.2018 в 04:06