How do I insert multiple rows of data from an HTML table into the database?

I want to create a attendance form with all the employee names in a HTML table from the database. Then the user has to enter certain values regarding each employee and then they have to save the data of all the employees into the database. Here is the table :

<table style="width:100%">
        <tr>
            <th>E_Code</th>
            <th>KjCode</th>
            <th>SecID</th>
            <th>Mkgs</th>
            <th>Akgs</th>
            <th>Ekgs</th>
            <th>Tkgs</th>
            <th width="100%">Name</th>
        </tr>
        <?php 
            for($i=1;$i<=mysqli_num_rows($sql1);$i++){ 
                $row = mysqli_fetch_array($sql1);
        ?>
        <tr>
            <td><input type="disabled" name="ecode<?php echo $i; ?>" class="input2" value="<?php $ecode = $row['ECode']; echo $ecode;?>" readonly></td>
            <td>
                <?='<select class="input" name="kjcode<?php echo $i; ?>" required>'.$dropOptions.'</select>' ?>
            </td>
            <td>
                <?='<select class="input" name="secid<?php echo $i; ?>" required>'.$dropOptions1.'</select>' ?>
            </td>
            <td><input class="input" type="number" name="mkgs<?php echo $i; ?>" placeholder="Mkgs"></td>
            <td><input class="input" type="number" name="akgs<?php echo $i; ?>" placeholder="Akgs" readonly></td>
            <td><input class="input" type="number" name="ekgs<?php echo $i; ?>" placeholder="Ekgs" readonly></td>
            <td><input class="input" type="number" name="tkgs<?php echo $i; ?>" placeholder="Tkgs"></td>
            <td width="100%"><input type="disabled" class="input2" value="<?=$row['EmpName'];?>" readonly></td>
            </tr>
        <?php }}?>
        </table>

I’m trying to insert the data into the database using this code :

if(isset($_POST['save'])){
        $size=sizeof($_POST);
        $number=$size/7;   //here 3 is number of column in the HTML table
        for($i=1;$i<=$number;$i++)
        {
            $index1="ecode".$i;
            $first[$i]=$_POST[$index1];
            $index2="kjcode".$i;
            $second[$i]=$_POST[$index2];
            $index3="secid".$i;
            $third[$i]=$_POST[$index3]; 
            /*$index4="mkgs".$i;
            $fourth[$i]=$_POST[$index4];
            $index5="akgs".$i;
            $fifth[$i]=$_POST[$index5];
            $index6="ekgs".$i;
            $sixth[$i]=$_POST[$index6];
            $index7="tkgs".$i;
            $seventh[$i]=$_POST[$index7];*/

           /* $save_query = mysqli_query($db, "insert into attendance(E_Code, KjCode, SecID, MKgs, AKgs, EKgs, TKgs, MuhuriID)
            values('$first[$i]', '$second[$i]', '$third[$i]', '$fourth[$i]', '$fifth[$i]', '$sixth[$i]', '$seventh[$i]', '$muhuri')");*/
            $save_query = mysqli_query($db, "insert into attendance(E_Code, KjCode, SecID, MuhuriID)
            values('$first[$i]', '$second[$i]', '$third[$i]', '$muhuri')");
        }
    }

However the code doesn’t work, nothing gets inserted into the database. I have tried with different methods but only the last table row gets inserted. I know the code is vulnerable to various attacks, but it will be working in a offline server so doesn’t matter much. If anyone can fix it’ll be nice. Please help me out. Thanks in advance.

You can start with getting rid of all the variables for nothing. After that, make the form field names arrays. I hope you only have a few employees. This will not scale well to edit a lot of employees all at once.

It always matters.

I have almost 300 employees and how do I make a field name array ?

The point about the number of employees relates to how easy it is to make a mistake when entering large numbers of values in a table. Once you have seen how to store the information, look at adding options to display them by department, or some other means of making the table smaller.

Your first query is commented out, but in your second query you use a variable called $muhuri, and I can’t see that you’ve defined that anywhere. So if your database table will not allow the MuhuriID to contain a NULL, then the query will fail. You should really check the value of $save_query once you’ve run the query, to see whether it worked or not.

This is something that @chorn was referring to:

$index1="ecode".$i;
$first[$i]=$_POST[$index1];

You create a variable called $index1 for no apparent reason, when your code could just read:

$first[$i]=$_POST["ecode".$i];

If you don’t use the $first[] array outside of this loop, though, there’s no point creating it, and you could just use flat variables. Obviously if you need it later, that’s no advantage, but if your form variables were named better, you could just re-use the $_POST array.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.