Reputation: 1925
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:
And this is what i need:
Upvotes: 1
Views: 980
Reputation: 9012
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");
}
?>
Upvotes: 1