set_charset("utf8"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Get user details $user = []; if (isset($_GET['id'])) { $user_id = $conn->real_escape_string($_GET['id']); $sql = "SELECT * FROM `".$GLOBALS['arif_users']."` WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); $stmt->close(); if (!$user) { die("User not found"); } } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { $id = $_POST['id']; $user_name = $_POST['user_name']; $user_phone = $_POST['user_phone']; $type = $_POST['type']; $user_id = $_POST['user_id']; $profilePicPath = $user['profile_pic']; // default old pic // Validate inputs if (empty($user_name) || empty($user_phone) || empty($user_id)) { $error = "All fields are required except password"; } elseif (!preg_match("/^[0-9]{10}$/", $user_phone)) { $error = "Invalid phone number format"; } else { // --- Handle Profile Picture Upload --- if (isset($_FILES['profile_pic']) && $_FILES['profile_pic']['error'] === UPLOAD_ERR_OK) { $uploadDir = __DIR__ . "/picture/"; if (!file_exists($uploadDir)) { mkdir($uploadDir, 0777, true); } $fileTmp = $_FILES['profile_pic']['tmp_name']; $fileName = time() . "_" . basename($_FILES['profile_pic']['name']); $filePath = $uploadDir . $fileName; if (move_uploaded_file($fileTmp, $filePath)) { $profilePicPath = "/picture/" . $fileName; // পুরনো ফাইল ডিলিট (যদি থাকে) if (!empty($user['profile_pic']) && file_exists(__DIR__ . "/" . $user['profile_pic'])) { unlink(__DIR__ . "/" . $user['profile_pic']); } } } // Update query if (!empty($_POST['password'])) { $password = password_hash($_POST['password'], PASSWORD_DEFAULT); $sql = "UPDATE `".$GLOBALS['arif_users']."` SET user_id = ?, user_name = ?, user_phone = ?, type = ?, password = ?, profile_pic = ? WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("ssssssi", $user_id, $user_name, $user_phone, $type, $password, $profilePicPath, $id); } else { $sql = "UPDATE `".$GLOBALS['arif_users']."` SET user_id = ?, user_name = ?, user_phone = ?, type = ?, profile_pic = ? WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("sssssi", $user_id, $user_name, $user_phone, $type, $profilePicPath, $id); } if ($stmt->execute()) { $success = "User updated successfully!"; // Refresh user data $sql = "SELECT * FROM `".$GLOBALS['arif_users']."` WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); $stmt->close(); } else { $error = "Error updating user: " . $conn->error; } } } ?>