You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
3.0 KiB
94 lines
3.0 KiB
<?php
|
|
session_start();
|
|
include 'connect.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
$user_id = $_SESSION['user_id'];
|
|
|
|
if (isset($_POST['update'])) {
|
|
// Get form data
|
|
$first_name = trim($_POST['first_name']);
|
|
$last_name = trim($_POST['last_name']);
|
|
$email = trim($_POST['email']);
|
|
$phone = trim($_POST['phone']);
|
|
$company_address = trim($_POST['company_address']);
|
|
|
|
|
|
try {
|
|
$conn->begin_transaction();
|
|
|
|
// 1. First update the users table
|
|
$query_users = "UPDATE users
|
|
SET firstname = ?, lastname = ?, email = ?, phone = ?
|
|
WHERE user_id = ?";
|
|
$stmt_users = $conn->prepare($query_users);
|
|
if (!$stmt_users) {
|
|
throw new Exception("Prepare failed for users table: " . $conn->error);
|
|
}
|
|
$stmt_users->bind_param("ssssi", $first_name, $last_name, $email, $phone, $user_id);
|
|
|
|
if (!$stmt_users->execute()) {
|
|
throw new Exception("Error updating users table: " . $stmt_users->error);
|
|
}
|
|
|
|
// 2. Then handle the Employers table
|
|
// First check if a record exists
|
|
$check_query = "SELECT COUNT(*) as count FROM employers WHERE user_id = ?";
|
|
$check_stmt = $conn->prepare($check_query);
|
|
$check_stmt->bind_param("i", $user_id);
|
|
$check_stmt->execute();
|
|
$result = $check_stmt->get_result();
|
|
$row = $result->fetch_assoc();
|
|
|
|
if ($row['count'] > 0) {
|
|
// Update existing record
|
|
$employer_query = "UPDATE employers
|
|
SET
|
|
company_address = ?
|
|
|
|
WHERE user_id = ?";
|
|
} else {
|
|
// Insert new record
|
|
$employer_query = "INSERT INTO employers
|
|
( company_address, user_id)
|
|
VALUES (?, ?)";
|
|
}
|
|
|
|
$stmt_employers = $conn->prepare($employer_query);
|
|
if (!$stmt_employers) {
|
|
throw new Exception("Prepare failed for job_seekers table: " . $conn->error);
|
|
}
|
|
|
|
// Same binding for both UPDATE and INSERT
|
|
$stmt_employers->bind_param("si",
|
|
$company_address,
|
|
$user_id
|
|
);
|
|
|
|
if (!$stmt_employers->execute()) {
|
|
throw new Exception("Error with job_seekers table: " . $stmt_employers->error);
|
|
}
|
|
|
|
$conn->commit();
|
|
$_SESSION['success_message'] = "Profile updated successfully!";
|
|
header("Location: employerprofile");
|
|
exit();
|
|
|
|
} catch (Exception $e) {
|
|
$conn->rollback();
|
|
$_SESSION['error_message'] = "Error: " . $e->getMessage();
|
|
error_log("Profile update error: " . $e->getMessage());
|
|
header("Location: employerprofile");
|
|
exit();
|
|
} finally {
|
|
if (isset($stmt_users)) $stmt_users->close();
|
|
if (isset($stmt_employers)) $stmt_employers->close();
|
|
if (isset($check_stmt)) $check_stmt->close();
|
|
$conn->close();
|
|
}
|
|
}
|
|
?>
|