1

我需要插入技术来将多个相同形式的数组插入到一个表中,我尝试了以下代码。

输入.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>

动作.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");
}
?>

我得到这个输出: 输出获取

这就是我需要的: 需要输出

4

1 回答 1

1

好吧,所以试试这个:

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

    ?>
于 2013-08-30T12:51:41.087 回答