<?php
include 'db.php';
$id = $_GET['id'];

$result = mysqli_query($conn, "SELECT * FROM user_record WHERE id=$id");
$row = mysqli_fetch_assoc($result);

if (isset($_POST['update'])) {
    $prefix = $_POST['prefix'];
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];

    if (!empty($_FILES['image']['name'])) {
        $image_name = $_FILES['image']['name'];
        $tmp_name = $_FILES['image']['tmp_name'];
        move_uploaded_file($tmp_name, "uploads/" . $image_name);
        $update = "UPDATE user_record SET prefix='$prefix', fullname='$fullname', email='$email', phone='$phone', image='$image_name' WHERE id=$id";
    } else {
        $update = "UPDATE user_record SET prefix='$prefix', fullname='$fullname', email='$email', phone='$phone' WHERE id=$id";
    }

    mysqli_query($conn, $update);
    header("Location: index.php");
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Edit Record</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<h2>Edit User</h2>
<form method="post" enctype="multipart/form-data">
    <label>Prefix:</label>
    <select name="prefix">
        <option <?= $row['prefix'] == 'Mr' ? 'selected' : '' ?>>Mr</option>
        <option <?= $row['prefix'] == 'Mrs' ? 'selected' : '' ?>>Mrs</option>
        <option <?= $row['prefix'] == 'Dr' ? 'selected' : '' ?>>Dr</option>
        <option <?= $row['prefix'] == 'Ms' ? 'selected' : '' ?>>Ms</option>
        <option <?= $row['prefix'] == 'Prof' ? 'selected' : '' ?>>Prof</option>
    </select><br>

    <label>Full Name:</label>
    <input type="text" name="fullname" value="<?= $row['fullname'] ?>"><br>

    <label>Email:</label>
    <input type="email" name="email" value="<?= $row['email'] ?>"><br>

    <label>Phone Number:</label>
    <input type="text" name="phone" value="<?= $row['phone'] ?>"><br>

    <label>Profile Picture:</label>
    <input type="file" name="image"><br>

    <button type="submit" name="update">Update</button>
</form>
</body>
</html>