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

96 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'] ?? '';
$area = $_POST['area'] ?? 0;
$start_date = $_POST['start_date'] ?? '';
if (empty($name) || empty($area) || empty($start_date)) {
$error = 'Por favor, preencha todos os campos.';
} elseif ($area <= 0) {
$error = 'A área deve ser maior que zero.';
} else {
try {
$pdo = db();
$stmt = $pdo->prepare('INSERT INTO crops (name, area, start_date) VALUES (?, ?, ?)');
$stmt->execute([$name, $area, $start_date]);
$success = 'Lavoura adicionada com sucesso!';
} catch (PDOException $e) {
$error = 'Erro ao adicionar lavoura: ' . $e->getMessage();
}
}
}
?>
<header class="top-bar">
<h1>Adicionar Lavoura</h1>
</header>
<main class="content">
<div class="card">
<div class="card-header">
Nova Lavoura
</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_crop.php" method="POST">
<div class="form-group">
<label for="name">Nome da Cultura (ex: Soja, Milho)</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="area">Área (em hectares)</label>
<input type="number" step="0.01" id="area" name="area" required>
</div>
<div class="form-group">
<label for="start_date">Data de Início</label>
<input type="date" id="start_date" name="start_date" required>
</div>
<button type="submit" class="btn">Adicionar</button>
</form>
<br>
<a href="crops.php">Voltar para Lavouras</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'; ?>