How to make a button submit a specific form, when using a while loop to create forms

hey guys, I am using a while loop to store my query results in forms, which I can make editable. When editing the data in the forms, I have a button under each one, but when I edit a form and press submit it doesn’t work, unless I edit the last form created, so obviously there is a problem doing this while looping.

Here is my query results loop:

while($row = sqlsrv_fetch_array($stmt))
		{
			$forename = $row['Forename'];
			$surname = $row['Surname'];
			$dob = $row['Date_of_Birth'];
			$school = $row['School'];
			$medical = $row['Medical_Notes'];
			$allergies = $row['Allergies'];
			$notes = $row['Notes'];
			$cid = $row['Child_ID'];
			
			$children.= '<fieldset class="childrenForm">
						<legend>Child</legend>

						<label>ID:</label> 
						<input type="text" name="cid" value=" '.$cid.'" readonly="readonly"/><br/><br/> 
						<label>Name:</label> 
						<input type="text" name="name" value=" '.$forename.'" '.$ro.'/><br/><br/> 
						<label>Surname:</label>
						<input type="text" name="surname"	value="'.$surname.'" '.$ro.'  /><br/><br/>
						<label>Date of Birth:</label> 
						<input type="text"	name="dob" value="'.$dob.'" '.$ro.'/><br/><br/>
						<label>School:</label> 
						<input type="text" name="school" value="'.$school.'" '.$ro.' /><br/><br/>
						<label>Medical:</label>
						<input type="text" name="medical" value="'.$medical.'" '.$ro.' /><br/><br/>
						<label>Allergies:</label> 
						<input type="text" name="allergies" value="'.$allergies.'" '.$ro.' /><br/><br/>
						<label>Notes:</label> 
						<input type="text" name="notes" value="'.$notes.'" '.$ro.'/><br/><br/>

						<input id="subButt" name="editChildren" type="submit" value="Save"/>
				
					</fieldset>';
		}

And here is the SQL to update the children table:

if(isset($_POST['editChildren']) && isset($_SESSION['fid']))
{
	$givenName=$_POST['name'];
	$familyName=$_POST['surname'];
	$dob=$_POST['dob'];
	$school=$_POST['school'];
	$medical=$_POST['medical'];
	$allergies=$_POST['allergies'];
	$notes=$_POST['notes'];
	$childID=$_POST['cid'];
	
	
	$sql = "UPDATE Children SET Forename=?, Surname=?, Date_of_Birth=?, School=?, Medical_Notes=?, Allergies=?, Notes=? where Family_ID =? AND Child_ID=? ";
	
	$params = array($givenName, $familyName, $dob, $school, $medical, $allergies, $notes, $_SESSION['fid'], $childID);
	
	//Check SQL Query
	$stmt = sqlsrv_query( $conn, $sql,$params, array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
	if(!$stmt)
	{
		die('An error has occured with your registration. If this is an indeliberate occurance,
			 please report this to us through the contact us page with details of the error.');
	}
	
	
	else{
	
		header('Location: fam_children.php');
	
	}
}

I think the problem is that I need to create a separate button for each form, I am not sure, if anyone can help me out that would be fantastic!

Thanks!!!

Hint: You can have more than one <form> tag on a page. A submit button will fire only the form which it is inside.

Hey Starlion, I was concentrating so much on the PHP, I left my HTML out in the rain. OOPS! I never had the form tags around the the looped form inputs, right enough. Sometimes I swear I cannot do any easy stuff, only hard stuff!
:slight_smile:

Thank you!