$track) { $trackTitle = trim($track['title']); // Restructure FILES array for easier access $audioFile = [ 'name' => $files['name']['audio_file'][$index], 'type' => $files['type']['audio_file'][$index], 'tmp_name' => $files['tmp_name']['audio_file'][$index], 'error' => $files['error']['audio_file'][$index], 'size' => $files['size']['audio_file'][$index] ]; if (!empty($trackTitle) && $audioFile['error'] == UPLOAD_ERR_OK) { $audioTmpName = $audioFile['tmp_name']; $audioName = basename($audioFile['name']); $audioExt = pathinfo($audioName, PATHINFO_EXTENSION); $newAudioName = uniqid('track_', true) . '.' . $audioExt; $audioFilePath = 'uploads/music/' . $newAudioName; if (move_uploaded_file($audioTmpName, $audioFilePath)) { try { $stmt = $pdo->prepare( "INSERT INTO tracks (user_id, title, album, genre, release_date, file_path, cover_art_path) VALUES (:user_id, :title, :album, :genre, :release_date, :file_path, :cover_art_path)" ); $stmt->execute([ ':user_id' => $userId, ':title' => $trackTitle, ':album' => $albumName, ':genre' => $genre, ':release_date' => $releaseDate, ':file_path' => $audioFilePath, ':cover_art_path' => $coverArtPath ]); } catch (PDOException $e) { $errorMessage = "Database error: " . $e->getMessage(); $allSucceeded = false; break; // Exit loop on DB error } } else { $errorMessage = "Failed to move uploaded audio file for track: " . htmlspecialchars($trackTitle); $allSucceeded = false; break; } } else { $errorMessage = "Please provide a title and audio file for all tracks. Error code: " . $audioFile['error']; $allSucceeded = false; break; } } if ($allSucceeded) { $successMessage = "Album and all tracks submitted successfully!"; } } elseif (empty($errorMessage)) { $errorMessage = "No tracks were submitted. Please add at least one track."; } } ?>