Im having a problem using foreach i cant seem to understand this yet.
my problem is when i enter all the data needed it shows many inserting code
I cant seem to figure this out

This is the Output

INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (1,1)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (1,2)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (1,3)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (1,4)

INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (2,1)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (2,2)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (2,3)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (2,4)

INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (3,1)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (3,2)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (3,3)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (3,4)

INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (4,1)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (4,2)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (4,3)
INSERT INTO `dependents`(`d_name`, `d_age`) VALUES (4,4)

This is the code

<?php
$dependents = $_POST['dependentsname'];
$age = $_POST['age'];

foreach ($dependents as $key1 => $value1){
//            $value =$dependents[$key1];
foreach ($age  as $key2 => $value2){
echo $insert_dependents="INSERT INTO `dependents`(`d_name`, `d_age`)
                                       VALUES ($value1,$value2)";
echo "<br>";
}
echo "<br>";
}
?>



    <div id="Dependents_left">
    <u>DEPENDENTS:</u>
    <table>
        <tr>
            <th>NAME</th>
            <th>AGE</th>
            <th>NAME</th>
            <th>AGE</th>
        </tr>
        <tr>
            <td>1. <input type="text" name="dependentsname[]"></td>
                    <td><input type="text" name="age[]"></td>
            <td>3. <input type="text" name="dependentsname[]"></td>
                    <td><input type="text" name="age[]"></td>  
        </tr>
        <tr>
            <td>2. <input type="text" name="dependentsname[]"></td>
            <td><input type="text" name="age[]"></td>
            <td>4. <input type="text" name="dependentsname[]"></td>
            <td><input type="text" name="age[]"></td>
        </tr>
    </table>

any help would be helpful

I think you might have resolved this, but if not then problem is that you iterating one array into another and hence it gives multiple time result.
Actully both array contains same number of values.
I mean logically first record

    dependents[0]="1st dependant Name"
    age[0]="1st dependant age"
    similarly second
    dependents[1]="2nd dependant Name"
    age[1]="2nd dependant age"
    So you will use for loop to access the index



<?php
$dependents = $_POST['dependentsname'];
$age = $_POST['age'];
for($i=0;$i<count($dependents);i++)
{
    echo $insert_dependents="INSERT INTO `dependents`(`d_name`, `d_age`)VALUES ($dependents[$i],$age[$i])";
    echo "<br>";
} 
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.