EDITAR UPDATE FOTO FORMULARIO PHP MYSQL API JSON

 <img :src="'foto/'+producto.foto" width="100">    



<input type="file" class="form-control" name="afoto" id="afoto" ref="afoto" v-on:change="everimg()" >



<input type="hidden" name="ffoto" id="ffoto" v-model="producto.foto">



///////////////////

$input = file_get_contents('php://input');

$decode = json_decode($input, true);


$articulo_id = $decode["a_id"];

$fnombre = $decode["fnombre"];

$lprecio = $decode["lprecio"];


//$class = $decode["class"];

//$city = $decode["city"];


$sql = "UPDATE articulos SET nombre = '{$fnombre}', 

precio = '{$lprecio}' WHERE id = {$articulo_id}";


if(mysqli_query($conn,$sql)){

echo json_encode(array('update' => 'success'));

}else{

echo json_encode(array('update' => 'failed'));

}




///////////////////////


$pdo=new PDO("mysql:dbname=selectfetch;host=localhost","root","root");


$pdo->query("SET NAMES 'UTF8'");


$id=$_POST['aid'];

$nombre=$_POST['anombre'];

$precio=$_POST['aprecio'];

$ffoto=$_POST['ffoto'];


$foto="";


if(isset($_FILES['afoto']['name'])){ 


$foto = time().'_'.rand(100000,999999).'.jpg';




$target_dir="foto/";



$target_file=$target_dir.basename($foto);


move_uploaded_file($_FILES['afoto']['tmp_name'],$target_file);


unlink($target_dir.$ffoto);// borra foto anterior




$a=$articulos->editar($id,$nombre,$precio,$foto);



$sql='UPDATE articulo SET nombre=:nombre, precio=:precio,foto=:foto WHERE id = :id';


$stmt = $this->conexion->prepare($sql);


$stmt->bindValue(":nombre",$nombre,PDO::PARAM_STR);

$stmt->bindValue(":precio",$precio,PDO::PARAM_STR);

$stmt->bindValue(":id",$id,PDO::PARAM_INT);


$stmt->bindValue(":foto",$foto,PDO::PARAM_STR);

$stmt->execute();



}else{



//$a=$articulos->editar($id,$nombre,$precio,$ffoto);



$sql='UPDATE articulo SET nombre=:nombre, precio=:precio,foto=:foto WHERE id = :id';


$stmt = $this->conexion->prepare($sql);


$stmt->bindValue(":nombre",$nombre,PDO::PARAM_STR);

$stmt->bindValue(":precio",$precio,PDO::PARAM_STR);

$stmt->bindValue(":id",$id,PDO::PARAM_INT);


$stmt->bindValue(":foto",$ffoto,PDO::PARAM_STR);



$stmt->execute();



}


/////////////////otro////////////////////////////


<img src="sin.png" id="fot" width="80">




    <input  type="file" id="sample_image" onchange="preview_image(event)">




var nombre = document.getElementById('nombre').value;

var precio = document.getElementById('precio').value;

var id = document.getElementById('id').value;




var fi = document.getElementById('sample_image');


let formData = new FormData();


formData.append("archivo", fi.files[0]); 

formData.append('nombre', nombre);

formData.append('precio', precio);

formData.append('id', id);


const result = await fetch("fetchEditProduct.php", {

    method: "POST",

    body:formData,


  /*  headers: {

      "Content-Type": "application/json",

    },

*/



  });


  const r = await result.json();


  console.log(r);


  console.log(fi.files[0].name);



  if(r.save == 'exito'){


console.log('con exito');




//alert("EDITO CON EXITO");




document.getElementById("aviso4").style.fontFamily = "Cooper Black";

document.getElementById("aviso4").innerHTML =  "Registro Actualizado con Exito...!!!";

document.getElementById("aviso4").style.display="block";


setTimeout(function(){

document.getElementById("aviso4").style.display="none";

}, 1500);




//Función para actualizar cada 5 segundos(5000 milisegundos)

//setInterval("actualizar()",1500);



//location.reload();


//cargar_tabla();


//cargar_tabla();


/*

 setInterval(function() {


                document.getElementsByTagName('table')[0].reload();


                }, 500);

*/




setInterval(function() {

  location.reload(); 

}, 1500);




  form.reset();




}else{

console.log('fallo');

}



document.getElementById("fot").src="sin.png";


  form.reset();



 //cerrar modal

//$('#modal1').modal('close');




cargar_tabla();




}



  }



});


////////////////json////////////////////


$nombre=$_POST["nombre"];

$precio=$_POST["precio"];

$id=$_POST["id"];


//$foto=$_FILES["archivo"]["name"];




$query = "update articulos set nombre='".$nombre."' , precio=".$precio."  where id=".$id."";

$sql = mysqli_query($conexion , $query);




$temp= $_FILES["archivo"]["tmp_name"];


$img_ext = pathinfo($_FILES["archivo"]["name"], PATHINFO_EXTENSION);



move_uploaded_file($temp, "fotos/".$id.'.'.$img_ext);




//move_uploaded_file($temp, "upload/" .$image_file);







/*

if (!$sql) {

    die('Failed.');

}

*/


//echo json_encode('Success');


//echo json_encode($query);




if($sql){

echo json_encode(array('save' => 'exito'));

}else{

echo json_encode(array('save' => 'fallo'));

}







////////////////////

function preview_image(event) 

{

 var reader = new FileReader();

 reader.onload = function()

 {

  var output = document.getElementById('fot');

  output.src = reader.result;

 }

 reader.readAsDataURL(event.target.files[0]);

}

Comentarios

Entradas populares de este blog

NUMEROS SERIE DE DISCO RIGIDO Y PENDRIVER USB CON PHP

INSERTAR Y UPDATE FECHA CON PHP MYSQL EN FORMULARIO METODO POST CON JQUERY ETIQUETA DATE

Copia de Seguridad de Archivo MS Access (.mdb) a Pendrive con VB6