prepare("SELECT * FROM credentials WHERE credential_id = ?"); $stmt->execute([$credential_id]); $credential = $stmt->fetch(PDO::FETCH_ASSOC); if (!$credential) { // Credential not found header('Location: dashboard.php'); exit; } $client_id = $credential['client_id']; // For redirecting back // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = trim($_POST['name']); $username = trim($_POST['username']); $password = $_POST['password']; // Don't trim password $url = trim($_POST['url']); $notes = trim($_POST['notes']); // Basic validation if (empty($name) || empty($username)) { $error = "Credential Name and Username are required."; } else { // If password is not changed, don't update it if (empty($password)) { $updateStmt = $pdo->prepare( "UPDATE credentials SET name = ?, username = ?, url = ?, notes = ?, updated_at = NOW() WHERE credential_id = ?" ); $updateStmt->execute([$name, $username, $url, $notes, $credential_id]); } else { $updateStmt = $pdo->prepare( "UPDATE credentials SET name = ?, username = ?, password = ?, url = ?, notes = ?, updated_at = NOW() WHERE credential_id = ?" ); $updateStmt->execute([$name, $username, $password, $url, $notes, $credential_id]); } log_audit_event('credential_update', $_SESSION['user_id'], 'credential', $credential_id); // Redirect back to the client detail page with a success message header("Location: dashboard.php?client_id=$client_id&status=credential_updated"); exit; } } $displayName = $_SESSION['user_display_name'] ?? 'User'; ?>