Updating form handler when new inputs added to form

I’ve got a form which uses PHP to add labels & corresponding inputs whenever items are added to a database. I need to dynamically add corresponding actions in my form handler as well, but I’m not sure where to start.

Example of the form code (error checking removed & code simplified for clarity):

$link = mysqli_connect('p:localhost', 'username', 'password');

$output = mysqli_query($link, 'SELECT id, name FROM pages ORDER BY name');

while ($row = mysqli_fetch_assoc($output)) {
	echo '<li><a href="?page=meal'.$row['id'].'" name="meal['.$row['id'].']" title="'.$row['name'].'">'.$row['name'].'</a></li>
	  <input id="mealSP-'.$row['id'].'" name="mealSP['.$row['id'].']" value="0" />
	  <label for="mealSP-'.$row['id'].'">Single Portion</label>
	  <input id="mealFP-'.$row['id'].'" name="mealFP['.$row['id'].']" value="0" />
	  <label for="mealFP-'.$row['id'].'">Family Pack</label>

and the relevant lines in the form handler should look like this:

$mealSP = $_POST['mealSP'];
$mealFP = $_POST['mealFP'];

$mealSP[0] = '	'.$mealSP[0].' Cheeseburger, SP<br />';
$mealFP[0] = '	'.$mealFP[0].' Cheeseburger, FP<br />';
$mealSP[1] = '	'.$mealSP[1].' Tamales, SP<br />';
$mealFP[1] = '	'.$mealFP[1].' Tamales, FP<br />';

if (preg_match('%^\\s0{1,2}%',$mealSP[0])) { // 0 or 00 resets value to null so it doesn't display in $message
	$mealSP[0] = '';

if (preg_match('%^\\s0{1,2}%',$mealFP[0])) {
	$mealFP[0] = '';

if (preg_match('%^\\s0{1,2}%',$mealSP[1])) {
	$mealSP[1] = '';

if (preg_match('%^\\s0{1,2}%',$mealFP[1])) {
	$mealFP[1] = '';

$message='Meals |'.$mealSP[0].$mealFP[0].$mealSP[1].$mealFP[1].'

so that for every item in the array, there’s a corresponding line in the form handler to prepend a tab and append the item name, followed by FP or SP, then a check to see if the value begins w/ 0 or 00, then echo the non-null items in $message.

Hope that’s clear. Any help appreciated.

Okay, here’s some preliminary work. Currently, it fails to increment the values from the sql query; all it does is retrieve the post values. Only the 1st item in the array appears, repeatedly.

$link = mysqli_connect('p:localhost', 'user', 'password');
// error checking here
$db = mysqli_select_db($link, 'dbname');
// error check
$output = mysqli_query($link, 'SELECT id, name FROM pages WHERE id > 200 AND id < 206 ORDER BY name');
//error check & echo errors

$rowTB = mysqli_fetch_assoc($output);

if (isset($_POST["mealSP"]) && is_array($_POST["mealSP"]) 
&& count($_POST["mealSP"]) > 0) 
		foreach ($_POST["mealSP"] as $mealSP) 
				$mealSP = '	'.$mealSP.' '.$rowTB['name'].', SP<br />';
				echo $mealSP; 

if (isset($_POST["mealFP"]) && is_array($_POST["mealFP"]) 
&& count($_POST["mealFP"]) > 0) 
		foreach ($_POST["mealFP"] as $mealFP) 
				$mealFP = '	'.$mealFP.' '.$rowTB['name'].', FP<br />';
				echo $mealFP; 


$message='<p><pre>Meals |'.$mealSP.$mealFP.'</pre></p>';
echo $message;

Questions: should I be using a ‘while loop’ instead of a ‘foreach’? how can i get both the submitted value & the queried value in the same statement?