I have a problem about updating the image in my database, I do not know if the WHERE condition in the UPDATE (upload_banner.php) is badly formulated or I lack code, if you could help me solve it, it would be very helpful, thank you very much !!!
producto.php
$sql_last=mysqli_query($con,"select LAST_INSERT_ID(id_producto) as last from products order by id_producto desc limit 0,1");
$rw=mysqli_fetch_array($sql_last);
$id_producto=intval($rw['last']);
$sql=mysqli_query($con,"select * from products where id_producto='$id_producto' limit 0,1");
//$id_producto=isset($_GET['id_producto']);
//$sql=mysqli_query($con,"select * from products where id_producto='$id_producto' limit 0,1");
$count=mysqli_num_rows($sql);
if ($count==0){
//header("location: bannerlist.php");
//exit;
}
$rw=mysqli_fetch_array($sql);
$images=$rw['images'];
<div align="center">
<h3><span class="glyphicon glyphicon-picture"></span> Imagen</h3>
<form class="form-vertical">
<div class="form-group">
<div class="col-sm-12">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="max-width: 100%;" >
<img class="img-rounded" src="imagenes/<?php echo $images;?>" />
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 250px; max-height: 250px;"></div>
<div>
<span class="btn btn-primary btn-file"><span class="fileinput-new">Selecciona una imagen</span>
<span class="fileinput-exists" onclick="upload_image();">Cambiar imagen</span><input type="file" name="fileToUpload" id="fileToUpload" required onchange="upload_image();"></span>
<a href="#" class="btn btn-danger fileinput-exists" data-dismiss="fileinput">Cancelar</a>
</div>
</div>
<div class="upload-msg"></div>
</div>
</div>
</form>
</div>
<script>
function upload_image(){
$(".upload-msg").text('Cargando...');
var id_producto=$("#id_producto").val();
var inputFileImage = document.getElementById("fileToUpload");
var file = inputFileImage.files[0];
var data = new FormData();
data.append('fileToUpload',file);
data.append('id_producto',id_producto);
$.ajax({
url: "ajax/upload_banner.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: data, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds
{
$(".upload-msg").html(data);
window.setTimeout(function() {
$(".alert-dismissible").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
}); }, 5000);
}
});
}
</script>
upload_banner.php
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_FILES["fileToUpload"]["type"])){
/* Llamar la Cadena de Conexion*/
include ("../config/db.php");
include ("../config/conexion.php");
$id_producto=intval($_POST['id_producto']);
$target_dir = "../imagenes/";
$carpeta=$target_dir;
if (!file_exists($carpeta)) {
mkdir($carpeta, 0777, true);
}
$target_file = $carpeta . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$errors[]= "El archivo es una imagen - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
$errors[]= "El archivo no es una imagen.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
$errors[]="Lo sentimos, la imagen ya existe.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 524288) {
$errors[]= "Lo sentimos, el archivo es demasiado grande. Tamaño máximo admitido: 0.5 MB";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$errors[]= "Lo sentimos, sólo archivos JPG, JPEG, PNG & GIF son permitidos.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$errors[]= "Lo sentimos, tu imagen no fue insertada.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$messages[]= "La imagen se ha sido insertado correctamente.";
$ruta=$_FILES["fileToUpload"]["name"];
$update=mysqli_query($con,"UPDATE products SET images='$ruta' WHERE id_producto='$id_producto'");
} else {
$errors[]= "Lo sentimos, hubo un error subiendo la imagen.";
}
}
if (isset($errors)){
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Error!</strong>
<?php
foreach ($errors as $error){
echo"<p>$error</p>";
}
?>
</div>
<?php
}
if (isset($messages)){
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Aviso!</strong>
<?php
foreach ($messages as $message){
echo"<p>$message</p>";
}
?>
</div>
<?php
}
}
?>