Inserting into a table with a foreign key

I am trying to insert audio media for users into tables with foreign keys. I have a user table that registers users , an album table that is supposed to store album information for each users album and albumsongs table that is supposed to store songs for an album in the album table.

This is my SQL

users table
userid| username | password | profilepicture
PK/AI userid

albums table
albumid | userid | albumpicture | albumname | albumgenre
PK/AI albumid     
FK userid to users

albumsongs table
songid | userid | albumid | songname | songpath
PK/AI songid   
FK userid to users  
FK albumid to albums

This is my PHP code to insert album info into album table:

   <?php
         if(isset($_POST["submit1"])) {

        $albumname = $_POST['albumname'];
        $albumgenre = $_POST['albumgenre'];
        $id = mysqli_insert_id($conn);
       
        $imagename = $_FILES['image']['name'];
        $tempName = $_FILES['image']['tmp_name'];
        $filepath = "albumpictures/".$imagename;
        
        if(move_uploaded_file($tempName, $filepath)) {
        }

        $qry = "SELECT userid FROM user WHERE userid = '$id'";
        
        $result = mysqli_query($conn, $qry);
        $num_rows = mysqli_num_rows($result);

        if($num_rows > 0) {
            $sql = "INSERT INTO albums (userid, albumname, albumpicture, albumgenre) VALUES ('$id', '$albumname', '$imagename', '$albumgenre')";
              $result = mysqli_query($conn, $sql);
    }
}
?>

This is my php code to insert into albumsongs table:

  <?php
        if(isset($_POST["submit2"])) {

            $songaname1 = filter_input(INPUT_POST, 'songaname1', FILTER_SANITIZE_SPECIAL_CHARS);
            $id = mysqli_insert_id($conn); // Get the last inserted album ID

            $audioname1 = $_FILES['audio1']['name'];
            $audio_tmp1 = $_FILES['audio1']['tmp_name'];
            $audio_folder1 = "albums/".$audioname;
            if(move_uploaded_file($audio_tmp1, $audio_folder1)) {
               $sql1 = "INSERT INTO albumsongs (userid, songaname1, audio1) VALUES ('$id', '$songaname1', '$audioname1')";
               $result1 = mysqli_query($conn, $sql1);
            }
        }
        ?>

I have tried using mysqli_insert_id.

I would like album info to be inserted into album table and songs for the album to be inserted into albumsongs.