php - How to collect values of input fields from a form displayed with a loop
I have a form in which input fields are displayed via a while loop with but I'm having issues with collecting the data from the input fields to save to the DB. What should be the possible php script to collect the form data?
if(isset($_GET['submit'])){
//collect form data
}
$sql="SELECT * FROM epl";
$result=mysqli_query($db_conx,$sql);
if($nr=mysqli_num_rows($result)>0){
These two variables are initialized as with 1 which is used in the while loop to increase the values will serve as the names of the input fields.
$Inum1=1;
$Inum2=1;
while($row=mysqli_fetch_array($result)){
$t1=$row['team1'];
$t2=$row['team2'];
echo '<form METHOD="get">
<div >
<div >
<div >
<label >'.$t1.'</label>
</div>
<input type="text" name="t_'.$Inum1.'_score" id="input">
</div>
<label >vs</label>
<div >
<input type="text" name="t_'.$Inum2.'_score" id="input">
<div >
<label>'.$t2.'</label>
</div>
</div>
</div> ';
$Inum1++;
$Inum2++;
}
echo ' <center><input name="submit" type="submit" value="ENTER NOW!"
style="width:30%; background-color:#379BFF; text-align:center;border:none; border-radius:3px; height:41px; color:#FFF;
font-size:24px; box-shadow:none; margin-top:20PX;">
</form>';
}
Answer
Solution:
As there is a form for every record and each form has it's own submit button they are effectively unique and separate entities on the page which means the field names can be repeated ( the ID however cannot )
The form is submitted and there will only be data for that row sent via the form submission - with predefined field names
t_1_score
andt_2_score
so it is simple to get the values from the $_GET array and use them in an update/insert statement. If it is an update statement then I think you will need a hidden field per form with that contains the ID for the db record.