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?

    //collect form data

$sql="SELECT * FROM epl";

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.

 echo '<form METHOD="get">
  <div >
  <div >
  <div >
   <label >'.$t1.'</label>
   <input type="text" name="t_'.$Inum1.'_score" id="input">

  <label >vs</label>

  <div >
  <input type="text" name="t_'.$Inum2.'_score" id="input">
  <div >

  </div> ';
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;">



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 namest_1_score andt_2_scoreso 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.

/* process form submission */
if( isset( $_GET['t_1_score'],$_GET['t_2_score'] ) ){

       As you submit only 1 form at a time the field names
       can be the same in each form

    /* pseudo database code */
    $sql='insert or update some table';
    $res=$db->query( $sql );

/* display your form and the input fields for each row */
if( $result ){
    while( $row=mysqli_fetch_array( $result ) ){

        echo "
        <form method='get'>
            <div class='block'>
                <div class='epl_form_g'>
                    <div class='eplT'><label>".$t1."</label></div>
                    <input type='text' name='t_1_score'>

                <label class='vs'>vs</label>

                <div class='epl_form_g'>
                    <input type='text' name='t_2_score'>
                    <div class='eplT'><label>".$t2."</label></div>
                <!--<input type='hidden' name='id' value='{$id}' />-->
                <input name='sub' 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 auto 0 auto; float:none;' />

People are also looking for solutions to the problem: php - Doctrine 2 removes parent row


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.