50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
$id = $_GET['id'];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
$name = $_POST['name'];
|
|
$division_id = $_POST['division_id'];
|
|
try {
|
|
$stmt = db()->prepare("UPDATE departments SET name = ?, division_id = ? WHERE id = ?");
|
|
$stmt->execute([$name, $division_id, $id]);
|
|
header('Location: user_management.php');
|
|
} catch (PDOException $e) {
|
|
echo "Error: " . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
try {
|
|
$stmt = db()->prepare("SELECT * FROM departments WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$department = $stmt->fetch();
|
|
|
|
$div_stmt = db()->query('SELECT id, name FROM divisions ORDER BY name');
|
|
$divisions = $div_stmt->fetchAll();
|
|
} catch (PDOException $e) {
|
|
echo "Error: " . $e->getMessage();
|
|
}
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Edit Department</title>
|
|
</head>
|
|
<body>
|
|
<h2>Edit Department</h2>
|
|
<form method="post">
|
|
<label>Department Name:</label>
|
|
<input type="text" name="name" value="<?= htmlspecialchars($department['name']) ?>" required>
|
|
<label>Division:</label>
|
|
<select name="division_id" required>
|
|
<?php foreach ($divisions as $division): ?>
|
|
<option value="<?= $division['id'] ?>" <?= $division['id'] == $department['division_id'] ? 'selected' : '' ?>><?= htmlspecialchars($division['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<button type="submit">Update</button>
|
|
</form>
|
|
<a href="user_management.php">Back to User Management</a>
|
|
</body>
|
|
</html>
|