34628-vm/add_product.php
2025-10-03 14:17:07 +00:00

94 lines
2.5 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
include 'includes/header.php';
include 'includes/sidebar.php';
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'] ?? '';
$unit = $_POST['unit'] ?? '';
$quantity = $_POST['quantity'] ?? 0;
if (empty($name) || empty($unit)) {
$error = 'Por favor, preencha o nome e a unidade do produto.';
} else {
try {
$pdo = db();
$stmt = $pdo->prepare('INSERT INTO products (name, unit, quantity) VALUES (?, ?, ?)');
$stmt->execute([$name, $unit, $quantity]);
$success = 'Produto adicionado com sucesso!';
} catch (PDOException $e) {
$error = 'Erro ao adicionar produto: ' . $e->getMessage();
}
}
}
?>
<header class="top-bar">
<h1>Adicionar Produto</h1>
</header>
<main class="content">
<div class="card">
<div class="card-header">
Novo Produto
</div>
<?php if ($error): ?><p class="error"><?php echo $error; ?></p><?php endif; ?>
<?php if ($success): ?><p class="success"><?php echo $success; ?></p><?php endif; ?>
<form action="add_product.php" method="POST">
<div class="form-group">
<label for="name">Nome do Produto</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="unit">Unidade (ex: kg, L, un)</label>
<input type="text" id="unit" name="unit" required>
</div>
<div class="form-group">
<label for="quantity">Quantidade Inicial</label>
<input type="number" step="0.01" id="quantity" name="quantity" value="0">
</div>
<button type="submit" class="btn">Adicionar</button>
</form>
<br>
<a href="inventory.php">Voltar para o Estoque</a>
</div>
</main>
<style>
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
.btn {
background-color: #27ae60;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.error { color: red; }
.success { color: green; }
</style>
<?php include 'includes/footer.php'; ?>