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

y53ybaqx  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(306)
<?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>

服务和活动的价值来自数据库,我想将所有服务和相关活动存储在数据库中 serviceactivitymap 表中,这是我的php代码,但当我存储数据时,最后一次检查的服务值仅与所有已检查的活动一起存储
非金属 MOXy
填满
返回
格林威治恒星时
填满
返回
如果incometax、gst、filling、return是复选框,那么当我们选中时:
国际贸易术语解释通则->填表、退货和商品及服务税->填表、退货
那么代码只存储
商品及服务税->填表、退货和商品及服务税->填表、退货
而不是:
国际贸易术语解释通则->填表、退货和商品及服务税->填表、退货

yqlxgs2m

yqlxgs2m1#

在代码中,每次循环在服务foreach中运行时,都会创建form start标记。试着跳出圈子
另外,您用于服务输入标记的名称 arr[1]["service"] 每次循环运行时都会被覆盖。相反,我建议您是否可以在每个循环中使用变量增量,如

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

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

    $i++;
 }

这将区分输入标记名,并可能正确保存。

rqenqsqc

rqenqsqc2#

你需要通过考试 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>

相关问题