35888-vm/add_installation.php
2025-11-20 22:14:33 +00:00

71 lines
2.8 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$client_name = trim($_POST['client_name'] ?? '');
$address = trim($_POST['address'] ?? '');
$technician_name = trim($_POST['technician_name'] ?? '');
$observations = trim($_POST['observations'] ?? '');
$voltage = trim($_POST['voltage'] ?? null);
$phase_neutral_ground = trim($_POST['phase_neutral_ground'] ?? null);
$breaker_output = trim($_POST['breaker_output'] ?? null);
$image_path = null;
if (empty($client_name) || empty($address) || empty($technician_name)) {
$_SESSION['message'] = 'Please fill all required fields.';
$_SESSION['message_type'] = 'danger';
header('Location: index.php');
exit;
}
// Handle file upload
if (isset($_FILES['installation_image']) && $_FILES['installation_image']['error'] === UPLOAD_ERR_OK) {
$upload_dir = __DIR__ . '/assets/uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0775, true);
}
$file_tmp_path = $_FILES['installation_image']['tmp_name'];
$file_name = $_FILES['installation_image']['name'];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$allowed_ext = ['jpg', 'jpeg', 'png', 'gif'];
if (in_array($file_ext, $allowed_ext)) {
$new_file_name = time() . '_' . basename($file_name);
$dest_path = $upload_dir . $new_file_name;
if (move_uploaded_file($file_tmp_path, $dest_path)) {
$image_path = 'assets/uploads/' . $new_file_name;
} else {
$_SESSION['message'] = 'Failed to move uploaded file.';
$_SESSION['message_type'] = 'danger';
header('Location: index.php');
exit;
}
} else {
$_SESSION['message'] = 'Invalid file type. Only JPG, JPEG, PNG, and GIF are allowed.';
$_SESSION['message_type'] = 'danger';
header('Location: index.php');
exit;
}
}
try {
$pdo = db();
$stmt = $pdo->prepare(
"INSERT INTO installations (client_name, address, technician_name, observations, voltage, phase_neutral_ground, breaker_output, image_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
);
$stmt->execute([$client_name, $address, $technician_name, $observations, $voltage, $phase_neutral_ground, $breaker_output, $image_path]);
$_SESSION['message'] = 'Installation registered successfully!';
$_SESSION['message_type'] = 'success';
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
$_SESSION['message'] = 'Failed to register installation. Please try again.';
$_SESSION['message_type'] = 'danger';
}
}
header('Location: index.php');
exit;