47 lines
1.8 KiB
PHP
47 lines
1.8 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$producto_id = $_POST['producto_id'] ?? null;
|
|
$texto_informativo = $_POST['texto_informativo'] ?? null;
|
|
$column_id = $_POST['column_id'] ?? null;
|
|
$imagen = $_FILES['imagen'] ?? null;
|
|
|
|
if (!$producto_id || !$texto_informativo || !$column_id || !$imagen || $imagen['error'] !== UPLOAD_ERR_OK) {
|
|
$_SESSION['error_message'] = 'Por favor, completa todos los campos, incluyendo la columna Kanban, y sube una imagen válida.';
|
|
header('Location: info_producto.php');
|
|
exit;
|
|
}
|
|
|
|
$upload_dir = 'assets/images/info_productos/';
|
|
if (!is_dir($upload_dir)) {
|
|
mkdir($upload_dir, 0777, true);
|
|
}
|
|
|
|
$imagen_nombre = uniqid('prod_', true) . '_' . basename($imagen['name']);
|
|
$imagen_path = $upload_dir . $imagen_nombre;
|
|
|
|
if (move_uploaded_file($imagen['tmp_name'], $imagen_path)) {
|
|
try {
|
|
$pdo = db();
|
|
$sql = 'INSERT INTO info_productos (producto_id, imagen_url, texto_informativo, column_id) VALUES (:producto_id, :imagen_url, :texto_informativo, :column_id)';
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute([
|
|
':producto_id' => $producto_id,
|
|
':imagen_url' => $imagen_path,
|
|
':texto_informativo' => $texto_informativo,
|
|
':column_id' => $column_id
|
|
]);
|
|
$_SESSION['success_message'] = 'Tarjeta de información creada exitosamente.';
|
|
} catch (PDOException $e) {
|
|
// If DB insert fails, delete the uploaded image
|
|
unlink($imagen_path);
|
|
$_SESSION['error_message'] = 'Error al guardar en la base de datos: ' . $e->getMessage();
|
|
}
|
|
} else {
|
|
$_SESSION['error_message'] = 'Error al subir la imagen.';
|
|
}
|
|
}
|
|
|
|
header('Location: info_producto.php');
|
|
exit; |