1

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

1 Answer 1

1

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");
    }

    ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.