Skip to content

Commit

Permalink
Agrega función editar img
Browse files Browse the repository at this point in the history
  • Loading branch information
Wlogzz committed Mar 12, 2023
1 parent 8afb889 commit 0c7f238
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
11 changes: 8 additions & 3 deletions app/peliculas/editaModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@
<label for="genero" class="form-label">Género</label>
<select class="form-select" name="genero" id="genero">
<option value="" selected disabled>Seleccione..</option>
<?php while ($row_genero = $generos -> fetch_assoc()) {?>
<?php while ($row_genero = $generos->fetch_assoc()) { ?>
<option value="<?= $row_genero['id']; ?>">
<?= $row_genero['nombre']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
</div>
<div class="row justify-content-center">
<div class="col-auto">
<img src="" alt="" id="img_poster" name="img_poster" width="200">
</div>
</div>
<div class="mb-3">
<label for="poster" class="form-label">Poster</label>
<input type="file" class="form-control" name="poster" id="poster" accept="image/jpeg">
</div>
<div class="row justify-content-end">
<div class="col-auto">
<button type="submit" class="btn btn-success">
<button type="submit" class="btn btn-success">
<i class="fa-solid fa-circle-check"></i> Actualizar
</button>
</div>
Expand Down
7 changes: 5 additions & 2 deletions app/peliculas/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@
</table>

<!-- Alert -->
<?php if (isset($_SESSION['msg'])) { ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php if (isset($_SESSION['msg']) && isset($_SESSION['color'])) { ?>
<div class="alert alert-<?= $_SESSION['color']; ?> alert-dismissible fade show" role="alert">
<?php echo $_SESSION['msg']; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php
unset($_SESSION['msg']);
unset($_SESSION['color']);
} ?>


Expand Down Expand Up @@ -100,6 +101,7 @@
let inputNombre = editaModal.querySelector('.modal-body #nombre')
let inputDescripcion = editaModal.querySelector('.modal-body #descripcion')
let inputGenero = editaModal.querySelector('.modal-body #genero')
let poster = editaModal.querySelector('.modal-body #img_poster')

//Ajax
let url = "getPelicula.php"
Expand All @@ -115,6 +117,7 @@
inputNombre.value = data.nombre
inputDescripcion.value = data.descripcion
inputGenero.value = data.id_genero
poster.src = '<?= $dir ?>' + data.id + '.jpg';
}).catch(err => console.log(err))
})

Expand Down
4 changes: 4 additions & 0 deletions app/peliculas/save.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
$id = $conn->insert_id;

$_SESSION['msg'] .= "<br>Registro guardo de forma exitosa!";
$_SESSION['color'] = "success";

// Verificar cargue imagen
if ($_FILES['poster']['error'] == UPLOAD_ERR_OK) {
Expand All @@ -36,12 +37,15 @@
}

if (!move_uploaded_file($_FILES['poster']['tmp_name'], $poster)) {
$_SESSION['color'] = "danger";
$_SESSION['msg'] .= "<br>Error al guardar la imágen";
}
} else {
$_SESSION['color'] = "warning";
$_SESSION['msg'] .= "<br>Formato de imágen no permitido";
}
} else {
$_SESSION['color'] = "danger";
$_SESSION['msg'] = "Error al guardar la imágen";
}
}
Expand Down
39 changes: 37 additions & 2 deletions app/peliculas/update.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
require('../config/db.php');

session_start();

$id = $conn -> real_escape_string($_POST['id']);
$nombre = $conn -> real_escape_string($_POST['nombre']);
$descripcion = $conn -> real_escape_string($_POST['descripcion']);
Expand All @@ -14,8 +16,41 @@

if ($conn -> query($sql)) {

$_SESSION['color'] = "success";
$_SESSION['msg'] .= "<br>Registro actualizado de forma exitosa!";

// Verificar cargue imagen
if ($_FILES['poster']['error'] == UPLOAD_ERR_OK) {
$permitidos = array("image/jpg", "image/jpeg");
if (in_array($_FILES['poster']['type'], $permitidos)) {

// Variable carpeta
$dir = "posters";

// Info de la imagen
$info_img = pathinfo($_FILES['poster']['name']);
$info_img['extension'];

// Relacionando los datos de la imagen con el id correspondiente al cargue
$poster = $dir . '/' . $id . '.jpg';

// Creando la carpeta de almacenamiento img
if (!file_exists($dir)) {
mkdir($dir, 0777);
}

if (!move_uploaded_file($_FILES['poster']['tmp_name'], $poster)) {
$_SESSION['color'] = "danger";
$_SESSION['msg'] .= "<br>Error al actualizar la imágen";
}
} else {
$_SESSION['color'] = "warning";
$_SESSION['msg'] .= "<br>Formato de imágen no permitido";
}
} else {
$_SESSION['color'] = "danger";
$_SESSION['msg'] = "Error al actualizar la imágen";
}
}

header('Location: index.php');

?>
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="p-5 mb-4 bg-light rounded-3">
<div class="container-fluid py-5">
<h1 class="display-5 fw-bold">
Bienvenid@ David
Bienvenid@
</h1>
<p class="col-md-8 fs-4">Using a series of utilities, you can create this jumbotron, just like the one in previous versions of Bootstrap. Check out the examples below for how you can remix and restyle it to your liking.</p>
<a class="btn btn-danger btn-lg" type="button" href="/app/peliculas/">
Expand Down

0 comments on commit 0c7f238

Please sign in to comment.