How to INSERT multiple rows with multiple values, but only for items that have checkboxes ticked next to them

I am coding a discography tool for a music database.

Artists are able to insert tracks, singles, EPs, and albums all into separate tables on the database.

Having tracks be in their own separate table allows the same tracks to be attached to multiple singles, EPs and albums while only requiring there to be one record for that track in the database.

Which means individual track pages can have an automatically generated list of links back to the Singles, EPs and albums that they appear on. Making navigating the database through the website a much smoother experience.

I have come to the point where I am coding a tool to attach any existing tracks in the database for a given artist onto an album page.

I am using another table in the database called ‘trackconnections’ to create the relational links between the track ids from the track table and the album id from the album table, with an additional column called albumtracknum available to be able to output the tracks in the right order when queried on the album page.

The code for the tool is behind a button labelled ‘Attach existing track(s) to album’. The code for this tool is as follows:

    if (isset($_POST['attachexistingtracktoalbum-submit'])) {
    
require "includes/db_connect.pdo.php";

$artistid = $_POST["artistid"];
$albumid = $_POST["albumid"];

echo '<strong>Select each track you would like to add to this album below and type in the track number you want it to have on the album in the box underneith the name of each selected track.</strong><br><br>';

$stmt = $pdo->query("SELECT * FROM track WHERE artist_id = '$artistid'
order by trackname");
while ($row = $stmt->fetch())
{           
    echo '<form action="includes/attachexistingtracktoalbum.inc.php" method = "post">';
    echo '<div class="checkbox">';
    echo '<label>';
    echo "<input type='checkbox' name='trackid[]' value='".$row['id']."' />";
        echo '<span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>';
    echo ' '.$row['trackname'];
    echo '</label>';
    echo "<br><label for='albumtracknum'>Track number:</label><br>
    <input type='text' name='albumtracknum[]'>
    <input type='hidden' name='albumid[]' value='".$albumid,"'>
    <br><br>";
    echo '</div>';

}
?>
  <input type="hidden" name="albumidforreturn" value="<?php echo $albumid;?>">
  <button type="submit" name="attachexistingtracktoalbum-submit">Attach track(s) to album</button>
  
<?php }
else {
header("Location: /index.php");
 exit();
}?>

(NB: The post data here is not sanitised for the sql query as it has been passed along in a hidden form from the original album page)

This generates a page with all track names for the current artist available on a list with checkboxes, with each track name being followed by a data entry box to enter the track number for the album being added to.

Submission of the form then hands off to the following include code:

    if (isset($_POST['attachexistingtracktoalbum-submit'])) {

require "db_connect.pdo.php";
 
 $albumid = implode(',',$_POST['albumid']);
 $trackid  = implode(',',$_POST['trackid']);
 $albumtracknum  = implode(',',$_POST['albumtracknum']);
 $albumidforreturn = $_POST['albumidforreturn'];

// echo 'albumid: '.$albumid.'<br>';
// echo 'trackid: '.$trackid.'<br>';
// echo 'albumtracknum: '.$albumtracknum.'<br>';
 
  $sql = "INSERT INTO trackconnections (albumid, trackid, albumtracknum) VALUES (?,?,?);";
  $stmt= $pdo->prepare($sql);
  $stmt->execute([$albumid,$trackid,$albumtracknum]);
    header("Location: ../albumdetail.php?albumid=$albumidforreturn");
    exit();
}
else {
header("Location: ../index.php");
 exit();
}

(NB: The commented out echos are there to test what the output is from the previous form)

The 2 problems I am having are that my ‘Attach existing tracks to album’ form submit:

1. Passes on too much data.

The generated form should only pass on the track ids, album number, and track numbers that have had their checkboxes ticked. For insertion into the ‘trackconnections’ table.

Instead it narrows down the ticked checkbox track ids only and then creates comma separated values for every available track to select, rather than just those actually selected.

Which leads to annoying outputs such as the following when passing on data from form to include:

albumid: 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
trackid: 30,14
albumtracknum: ,2,3,,,,,,,,,,,,,,,,,,,,,

Where it should only read as:

albumid: 4,4
trackid: 30,14
albumtracknum: 2,3

Having too much data get passed through means that the row inserts won’t be correct on multiple INSERTS once I do get this working, as they won’t align with one another in the correct order.

2. The include only INSERTS 1 row to the ‘trackconnections’ table.

It seems I am misunderstanding how to add multiple rows to the database with my code here.

As having multiple checkboxes ticked on my ‘Attach existing tracks to album’ form only inserts 1 single row to the database on submission of the form each time.

Consistently the only track that gets added to the ‘trackconnections’ table is the first track with its checkbox ticked and, because of issue no. 1 above, the albumtracknum is always 0 unless I type a number into the first albumtracknum box on the available checklist.

I need to make tweaks to this code so that both problem 1 and 2 are addressed together, meaning that ticking the checkboxes & adding track numbers into each box following the track names actually adds multiple rows to the database along with their corresponding album track numbers.

I hope someone can help.

EDIT TO SHOW REFINED AND WORKING CODE:

New code for checkbox and textbox sections –

if (isset($_POST['attachexistingtracktoalbum-submit'])) {
    
require "includes/db_connect.pdo.php";

$artistid = $_POST["artistid"];
$albumid = $_POST["albumid"];

echo '<strong>Select each track you would like to add to this album below and type in the track number you want it to have on the album in the box underneith the name of each selected track.</strong><br><br>';

$stmt = $pdo->query("SELECT * FROM track WHERE artist_id = '$artistid'
order by trackname");

echo '<form action="includes/attachexistingtracktoalbum.inc.php" method = "post">';

while ($row = $stmt->fetch())
{           
    
    echo '<div class="checkbox">';
    echo '<label>';
    echo "<input type='checkbox' name='trackid[]' value='".$row['id']."' />";
        echo '<span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>';
    echo ' '.$row['trackname'];
    echo '</label>';
    echo "<br><label for='albumtracknumber'>Track number:</label><br>
    <input type='text' name='albumtracknumber_".$row['id']."'>
    <input type='hidden' name='albumid[]' value='".$albumid,"'>
    <br><br>";
    echo '</div>';

}
?>
  <input type="hidden" name="albumidforreturn" value="<?php echo $albumid;?>">
  <button type="submit" name="attachexistingtracktoalbum-submit">Attach track(s) to album</button>
  </form>
  
<?php }
else {
header("Location: /index.php");
 exit();
}

New code for the include INSERT processing –

    if (isset($_POST['attachexistingtracktoalbum-submit'])) {

    require "db_connect.pdo.php";
     
     $albumid = implode(',',$_POST['albumid']);
     $trackid  = implode(',',$_POST['trackid']);
     $albumidforreturn = $_POST['albumidforreturn'];
     
foreach($_POST['trackid'] as $trackidloop) {
    
$albumtracknum = $_POST["albumtracknumber_{$trackidloop}"];

$sql = "INSERT INTO trackconnections (albumid, trackid, albumtracknum) VALUES (?,?,?);";
      $stmt= $pdo->prepare($sql);
      $stmt->execute([$albumid,$trackidloop,$albumtracknum]);
}
        header("Location: ../albumdetail.php?albumid=$albumidforreturn");
        exit();
    }
    else {
    header("Location: ../index.php");
     exit();
    }

Answer

This isn’t an entire solution but I see some problems: You are looping through tracks and creating a new form for each one. The first problem is , you are missing the closing form tag. I guess the browser is automatically creating one, when it sees the next form start tag. ?? That’s why you only get one single posted checkbox. I would put all the track checkboxes into a single form. Then the posted trackid[] array will contain all the checked items.

[EDIT after your comment: The hidden fields albumid[] post the entire array, whereas the trackid[] checkboxes only post the actual checked boxes (HTML spec).

Instead of having albumid[], You could put the trackID and albumID together for the checkbox value, then parse them apart when you handle the post: $value = $row['id']. ',' . $row['albumid']; echo "<input type='checkbox' name='trackid[]' value='".$value."' />";

ALSO, the SQL, “INSERT INTO (..) .. VALUES (…) ” only inserts one row.
It’s easy to do that SQL in a loop for all the checked boxes.
foreach($_POST['trackid'] as $value) { // parse the $value... // SQL Insert... }

EDIT 2: From my own comment: Like hidden fields, input (text) field arrays also post the entire array (with empty values for blank inputs). (Again, this is not a PHP thing, it’s a web browser standard to only post checked checkboxes and radio buttons. But ALL text and hidden INPUTs are posted.) So in your example, you need to code a mechanism to know which textbox goes with each checkbox. Quick and dirty…You could add a row index (0,1,2,3…) as another comma-separated number in your checkbox values, then you’ll have the index into the posted textbox array. Alternatively, you could name the textboxes ' .. name="textinput_' . $row['trackid'] . '" ...' (not an array), then upon post, read them in your foreach loop with
$val = $_POST["textinput_{$trackid}"];