65 lines
2.3 KiB
PHP
65 lines
2.3 KiB
PHP
<?php
|
|
$pageTitle = "Agregar Nuevo Producto";
|
|
require_once 'layout_header.php';
|
|
require_once 'db/config.php';
|
|
|
|
$message = '';
|
|
$error = '';
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$nombre_producto = filter_input(INPUT_POST, 'nombre_producto', FILTER_SANITIZE_STRING);
|
|
|
|
if ($nombre_producto) {
|
|
try {
|
|
$pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8", DB_USER, DB_PASS);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// Insertar el nuevo producto
|
|
$stmt = $pdo->prepare("INSERT INTO products (nombre) VALUES (:nombre)");
|
|
$stmt->execute(['nombre' => $nombre_producto]);
|
|
|
|
$message = "¡Producto '" . htmlspecialchars($nombre_producto) . "' agregado correctamente!";
|
|
|
|
} catch (PDOException $e) {
|
|
$error = "Error al agregar el producto: " . $e->getMessage();
|
|
}
|
|
} else {
|
|
$error = "Por favor, ingrese el nombre del producto.";
|
|
}
|
|
}
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<div class="row">
|
|
<div class="col-lg-6 mx-auto">
|
|
|
|
<?php if ($message): ?>
|
|
<div class="alert alert-success" role="alert">
|
|
<?php echo $message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo htmlspecialchars($error); ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<i class="fa fa-plus"></i> Agregar Nuevo Producto al Catálogo
|
|
</div>
|
|
<div class="card-body">
|
|
<form action="agregar_producto.php" method="post">
|
|
<div class="mb-3">
|
|
<label for="nombre_producto" class="form-label">Nombre del Producto</label>
|
|
<input type="text" class="form-control" id="nombre_producto" name="nombre_producto" required>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100"> <i class="fa fa-plus-circle"></i> Guardar Producto</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'layout_footer.php'; ?>
|