当我插入值时,只会存储最后一个值

时间:2018-08-02 09:52:25

标签: php mysql

<?php 
include("connection.php");

// Collect services.
$serviceid = "select * from service";
$servicevalue = $conn->query($serviceid) or die ($conn->error.__LINE__);
$services = [];
while ($row = $servicevalue->fetch_assoc()) {
    $services[] = $row;
}

// Collect activities.
$activityid = "select * from activity";
$activityvalue = $conn->query($activityid) or die ($conn->error.__LINE__);
$activities = [];
while ($row = $activityvalue->fetch_assoc()) {
    $activities[] = $row;
}

// Collect something else?
$perid = "select * from periodicity";
$pervalue = $conn->query($perid) or die ($conn->error.__LINE__);
$pers = [];
while ($row = $pervalue->fetch_assoc()) {
    $pers[] = $row;
}

foreach ($services as $service):

?>

    <form method="post" action="doalert.php" name="test">  
        <ul>
            <li>
                <input name='arr[1][service]' type='checkbox' data-id='Incometax'value="<?php echo $service['service_id']?>"/><?php echo $service['servicename']?>
                <ol>
                    <li>
                        <?php foreach ($activities as $activity) : ?>

                            <input type='checkbox' name='arr[1][activity][]' value="<?php echo $activity['activity_id']?>" /><?php echo $activity['nameofactivity'];?>
                    </li>
                    <br>
                    <?php endforeach;?>

                </ol>

            </li>
        </ul>           
        <br>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit"/>
</form>

服务和活动的价值来自数据库,我想将所有服务和相关活动存储在DB serviceactivitymap表中,这是我的php代码,但是当我存储数据时,仅检查最后一次服务价值存储所有选中的活动

  • 所得税
    • 填充
    • 返回
  • gst
    • 填充
    • 返回

如果所得税,消费税,进项税,退还税是复选框,则在我们进行检查时:

  

incometax->填充,返回和gst->填充,返回

然后代码仅存储

  

gst->填充,返回和gst->填充,返回

代替:

  

incometax->填充,返回和gst->填充,返回

2 个答案:

答案 0 :(得分:2)

您需要将service_id传递给活动名称,例如:

<input type='checkbox' name='activity[<?php echo $service['service_id']?>][]' value="<?php echo $activity['activity_id']?>" /><?php echo $activity['nameofactivity'];?>

完整形式如下:

<form method="post" action="doalert.php" name="test"> 
    <?php foreach ($services as $service): ?>

        <ul>
            <li>
                <input name='service[]' type='checkbox' data-id='Incometax' value="<?php echo $service['service_id']?>"/><?php echo $service['servicename']?>
                <ol>
                    <?php foreach ($activities as $activity): ?>
                        <li>
                            <input type='checkbox' name='activity[<?php echo $service['service_id']?>][]' value="<?php echo $activity['activity_id']?>" /><?php echo $activity['nameofactivity'];?>
                        </li>
                    <?php endforeach;?>
                </ol>
            </li>
        </ul>
    <?php endforeach;?>
    <input type="submit" name="submit" value="Submit"/>
</form>

答案 1 :(得分:0)

在您的代码中,每次循环运行时都会创建表单开始标签,因为循环位于服务foreach内部。尝试将其带出循环

此外,每次循环运行时,您用于服务输入标签的名称为arr[1]["service"]将被覆盖。相反,我建议您是否可以在每个循环中使用可变增量,例如

$i=0;
foreach($services as $service)
{
    .....

    <input name='arr[$i][service]' ...... 

    $i++;
 }

这将区分输入的标签名称,并且保存可能会正确进行。