File "edit_profile.php"

Full Path: /home/ccipcixf/public_html/miportal/edit_profile.php
File size: 3.08 KB
MIME-type: text/x-php
Charset: utf-8

<?php
session_start();
include("admin/includes/config.php");

// Check if form is submitted
if (isset($_POST['btn_submit'])) {
    $user_id = $_SESSION['user_acc_id'];

    // Retrieve data from POST request
    $fullname = isset($_POST['fullname']) ? trim($_POST['fullname']) : null;
    $phone = isset($_POST['user_phone']) ? trim($_POST['user_phone']) : null;
    $gender = isset($_POST['user_gender']) ? $_POST['user_gender'] : null;
    $dob = isset($_POST['user_dob']) ? $_POST['user_dob'] : null;
    $address = isset($_POST['user_address']) ? trim($_POST['user_address']) : null;
    $age = isset($_POST['user_age']) ? $_POST['user_age'] : null;

    // Fetch the current user data
    $stmt = $pdo->prepare("SELECT fullname, phone, gender, dob, address, age, user_image FROM tb_user WHERE user_id = ?");
    $stmt->execute([$user_id]);
    $current_user_data = $stmt->fetch(PDO::FETCH_ASSOC);

    // Handle profile image upload
    $image_updated = false;
    if ($_FILES['profile_image']['error'] == UPLOAD_ERR_OK) {
        $image_name = $_FILES['profile_image']['name'];
        $image_tmp_name = $_FILES['profile_image']['tmp_name'];
        $image_ext = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
        $allowed_extensions = ['jpg', 'jpeg', 'png'];

        // Validate image format
        if (!in_array($image_ext, $allowed_extensions)) {
            $_SESSION['error'] = "Invalid image format. Only JPG, JPEG, and PNG are allowed.";
            header("Location: profile_page.php");
            exit;
        }

        // Save the image with a new name
        $new_image_name = "profile_" . $user_id . "." . $image_ext;
        $upload_dir = "uploads/profile_images/";
        move_uploaded_file($image_tmp_name, $upload_dir . $new_image_name);
        $image_updated = true; // Track that the image was updated
    } else {
        // If no image was uploaded, use the existing one
        $new_image_name = $current_user_data['user_image'];
    }

    // Check if the form input values are different from the current database values
    $is_data_updated = (
        $fullname !== $current_user_data['fullname'] ||
        $phone !== $current_user_data['phone'] ||
        $gender !== $current_user_data['gender'] ||
        $dob !== $current_user_data['dob'] ||
        $address !== $current_user_data['address'] ||
        $age !== $current_user_data['age'] ||
        $image_updated
    );

    if ($is_data_updated) {
        // Update the user's profile in the database
        $stmt = $pdo->prepare("UPDATE tb_user SET fullname = ?, phone = ?, gender = ?, dob = ?, address = ?, age = ?, user_image = ? WHERE user_id = ?");
        $stmt->execute([$fullname, $phone, $gender, $dob, $address, $age, $new_image_name, $user_id]);

        // Set a success message in session
        $_SESSION['success'] = "Profile updated successfully!";
    }

    // Redirect to profile page
    header("Location: profile_page.php");
    exit;
} else {
    // If form not submitted, redirect to profile page
    header("Location: profile_page.php");
    exit;
}