File: /var/www/w230/html/election/admin/edit_candidate.php
<?php
include('../config/connect.php');
session_start();
if (!isset($_SESSION['admin'])) {
header("Location: login.php");
exit;
}
$id = $_GET['id'];
$candidate = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM candidates WHERE id = $id"));
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$number = $_POST['number'];
if ($_FILES['photo']['name']) {
$img = $_FILES['photo']['name'];
$tmp = $_FILES['photo']['tmp_name'];
move_uploaded_file($tmp, "../assets/img/$img");
} else {
$img = $candidate['photo'];
}
$sql = "UPDATE candidates SET name='$name', photo='$img', number='$number' WHERE id=$id";
mysqli_query($conn, $sql);
header("Location: candidate_list.php");
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="UTF-8">
<title>แก้ไขผู้สมัคร</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
<h3>แก้ไขผู้สมัคร</h3>
<form method="post" enctype="multipart/form-data">
<div class="mb-3">
<label class="form-label">ชื่อ</label>
<input type="text" name="name" value="<?= $candidate['name'] ?>" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">หมายเลข</label>
<input type="number" name="number" value="<?= $candidate['number'] ?>" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">เปลี่ยนรูปภาพ</label><br>
<img src="../assets/img/<?= $candidate['photo'] ?>" width="80" class="mb-2"><br>
<input type="file" name="photo" class="form-control">
</div>
<button class="btn btn-primary">อัปเดต</button>
<a href="candidate_list.php" class="btn btn-secondary">ยกเลิก</a>
</form>
</div>
</body>
</html>