Update in database (pdf) to mysql with PHP help

0

I already tried many things

updatepdf.php

<?php
$archivo=$_FILES["pdf"]["name"];
  $tipo = $_FILES["pdf"]["type"];
    $tamanio = $_FILES["pdf"]["size"];
$ruta=$_FILES["pdf"]["tmp_name"];
$destino="cv/".$archivo;
//copy ($ruta, $destino);
    //conexión a la base de datos
            $con = mysqli_connect("localhost", "root", "", "bd");
            if (mysqli_connect_errno()){
                echo "No se pudo conectar a la base de datos" .mysqli_connect_error();
            }       
$sid = mysqli_real_escape_string($con, $_POST["s_id"]);
            //insertamos los valores del formulario en nuestra bd
$sql = "UPDATE alumnos SET cvalu = '$destino' WHERE id_a = $sid";
            if (!mysqli_query($con,$sql)) {
                    die('Error: ' . mysqli_error($con));
                } 
                else{ 
echo " <div align='center' class='alert alert-success '>Isertados con exito</div>";     
                    }
        ?>

formular.html

<form enctype='multipart/form-data' id='formsubpdf'>

                    <label class='control-label'>Select File</label>
<div class='form-group'>
  <input id='file-0d' class='file' type='file' name='pdf' accept='application/pdf' >
        </div>

 <input type='text' required readonly class='form-control' name='s_id' id='idemp' value='$_SESSION['ses_id']' style='visibility:hidden; height:5px;'>             

            </form>

function.js

$(document).ready(function() {
    $(document).on('submit', '#formsubpdf', function() { 
console.log($(this).serialize()  ); 
        //obtenemos datos.
        var data = $(this).serialize();  

        $.ajax({  
            type : 'POST',
            url  : 'subirpdf.php',
            data:  new FormData(this),
            contentType: false,
                  cache: false,
            processData:false,

            success :  function(data) {  
                $('#formsubpdf')[0].reset();
                $("#cargando").html(data);


            }
        });

        return false;
    });

});

This is what I use in that I am wrong or that I am missing and I already have a headache

    
asked by ivanrangel 30.06.2017 в 01:41
source

1 answer

0

1) In the query you are treating the id_a as an integer, and yet you are escaping it to string with mysql_real_escape_string. Therefore: if the data type is a string (or varchar, in the DB), the quotes are missing in the query:

"UPDATE alumnos SET cvalu = '$destino' WHERE id_a = '$sid'"

In any case, it is not very elegant that the primary key is a string. If the id_a, on the contrary, is an int (or a numerical value) I recommend you to escape it as:

$sid = (int)$_POST["s_id"];

or something like that.

2) The file is not going up anywhere. You have to upload the file to the server ... Something like:

if(!move_uploaded_file($_FILES["pdf"]["tmp_name"],$destino)) {
    die("Error al subir el archivo");
}

3) As a recommendation, you need to mop up some mistakes. $ _FILES comes with an "error" value, in which it specifies if there was an error when uploading the file (for example, that you exceeded the maximum size limit established in the php.ini, or something similar) . If $ _FILES has that error, or if I in the form think about changing (with chrome developer tools, for example) the name of the file field in the form before posting, the application will throw you many errors . In addition, you will be doing update on uqe files that were not uploaded or that gave error. If you do not have problems with that, jewel. If not, you could do something like:

if(isset($_FILES['pdf']) && !$_FILES['pdf']['error']) {

    // etc...

} else {
    // Estrategia por si falló el archivo o no está seteado $_FILES['pdf']
}

The UPDATE does it whenever you are SURE that the file was uploaded. If you passed ALL the validations, then do the update.

Your final code, should be more or less like that (It's not very friendly to say, but based on the example, I think it works):

<?php
    if(isset($_FILES["pdf"]) && !$_FILES["pdf"])) {
        $archivo=$_FILES["pdf"]["name"];
        $tipo = $_FILES["pdf"]["type"];
        $tamanio = $_FILES["pdf"]["size"];
        $ruta=$_FILES["pdf"]["tmp_name"];
        $destino="cv/".$archivo;

        if(move_uploaded_file($_FILES["pdf"]["tmp_name"], $destino)) {
            //conexión a la base de datos
            $con = mysqli_connect("localhost", "root", "", "bd");
            if (mysqli_connect_errno()){
                echo "No se pudo conectar a la base de datos" .mysqli_connect_error();
            }       
            $sid = mysqli_real_escape_string($con, $_POST["s_id"]);
            //insertamos los valores del formulario en nuestra bd
            $sql = "UPDATE alumnos SET cvalu = '$destino' WHERE id_a = $sid";
            if (!mysqli_query($con,$sql)) {
                die('Error: ' . mysqli_error($con));
            } 
            else { 
                echo " <div align='center' class='alert alert-success '>Isertados con exito</div>";     
            }
        } else {
            echo "Hubo un error en la subida del archivo.";
        }
    } else {
        if($_FILES["pdf"]['error'])) {
            echo "No se pudo subir el archivo. Razón: ". $_FILES["pdf"]['error'];
        } else {
            echo "No se pudo subir el archivo por alguna razón desconocida";
        }
    }
?>

Greetings and luck!

    
answered by 30.06.2017 / 05:28
source