Faisal Faisal - 1 month ago 11
MySQL Question

Insert multiple arrays of same form into one table using loop

I need insertion technique for inserting multiple arrays of same form into one table, I've tried the following code.

input.html

<form method="post" enctype="multipart/form-data" action="action.php">

<div>
<input type="checkbox" name="feel[]" value="Worried" id="feel1">
<label for="feel1">Worried</label>
</div>

<div>
<input type="checkbox" name="feel[]" value="Scared" id="feel2">
<label for="feel2">Scared</label>
</div>
<div>
<input type="checkbox" name="problem[]" value="Worried" id="problem1">
<label for="problem1">Worried</label>
</div>

<div>
<input type="checkbox" name="problem[]" value="Scared" id="problem2">
<label for="problem2">Scared</label>
</div>

</form>


action.php

<?include 'include/connect.php';
for($i=0;$i<count($_POST["feel"]);$i++)
{
$array=array("self_assessment_emotion"=>$_POST['feel'][$i]);
$feel = $db_obj->insert($array,"tbl_self_assessment");
}
for($i=0;$i<count($_POST["problem"]);$i++)
{
$array=array("self_assessment_physical"=>$_POST['problem'][$i]);
$problem = $db_obj->insert($array,"tbl_self_assessment");
}
?>


I'm getting this output:
Output Getting

And this is what i need:
Output Required

Answer

All right, so try this:

    <?include 'include/connect.php';
    $max_number = max(count($_POST["feel"]), count($_POST["problem"]));
    for($i=0; $i < $max_number; $i++)
    {
    $array=array(
        "self_assessment_emotion"=>$_POST['feel'][$i],
        "self_assessment_physical"=>$_POST['problem'][$i]
    );
    $feel_problem = $db_obj->insert($array,"tbl_self_assessment");
    }

    ?>