向mysql数据库表提交复杂的动态表单

问题描述 投票:0回答:1

我正在尝试提交一份用于消防部门设备日常检查的表格。该表单是一个 while 循环,显示检查的名称以及通过、警告和失败的 3 个按钮。每个检查还有一个评论按钮,单击时会弹出一行并允许用户输入评论。

切记!!!每种形式都是不同的。每辆卡车都有不同的支票和不同数量的支票。有些卡车最多可以有 200 张支票。我需要这个适用于每辆卡车。

每当我提交表单时,我都会收到 PHP 错误:

Undefined array key "appnamesubmit"
 
Undefined array key "date"

Array to string conversion

这是我完成网络应用程序的最后一步。我只需要能够将卡车检查表单数据提交到 mysql 数据库表。

这是其中一辆卡车的卡车检查表:

这是我的表(truck_check_log)我愿意重新设计/重命名表中的列。:

这是我的表单代码:

<div class="container-xxl flex-grow-1 container-p-y">
             
<h2 align="center"><?php echo $appname?></h2><br />
     <?php date_default_timezone_set('America/New_York'); $date = date("m-d-Y H:i");?>
     
   <div class="form-group">
        <form name="add_name" id="add_name" method=POST action=truck-check-submit.php>
                    
   <div class="table-responsive">
      <table class="table table-bordered" id="dynamic_field">
        
   <?php while ($checks = mysqli_fetch_array($checksqry)) {
            echo '<input type=hidden name=xid[] value='. $checks['id']. '>';
            echo '<tr>';
            echo '<td style="width: 20%">'.$checks['check_name'].' <input type=hidden name=checkname id=checkname value='.$checks['check_name'].'> <br><br> 
            <input type="radio" class="btn-check" name="btn['. $checks['id']. ']" id="btnpass[' . $checks['id'] . ']" value="Pass">
            <label class="btn rounded-pill btn-outline-success" for="btnpass[' . $checks['id'] . ']">Pass</label>&Tab;&Tab;

            <input type="radio" class="btn-check" name="btn['. $checks['id'] . ']" id="btnwarning[' . $checks['id'] .']" value="Warning">
           <label class="btn rounded-pill btn-outline-warning" for="btnwarning[' . $checks['id'] . ']">Warning</label>&Tab;&Tab;

            <input type="radio" class="btn-check" name="btn['. $checks['id'] .']" id="btnfail[' . $checks['id'] . ']" value="Fail">
           <label class="btn rounded-pill btn-outline-danger" for="btnfail[' . $checks['id'] . ']">Fail</label>&Tab;&Tab;

           <button onclick="myFunction('. $checks['id'] . ')" type="button" name="comment[' . $checks['id'] . ']" id="comment[' . $checks['id'] . ']" class="btn btn-info" style="float: right"><img src="assets/comment.png" width="20px"></button> 
<br><br>
           <div style="display:none;" id="commentlinediv[' . $checks['id'] . ']">
           <input type="text" name=xcomment[] class="text-line" placeholder="Type Comment Here" id="commentline[' . $checks['id'] . ']"/>
</div>
</td>'; 

echo '<tr>';}?>

 </table>
<input placeholder="<?php echo $appname ?>" name="appnamesubmit"  id="appnamesubmit" hidden="hidden"><br>
<input placeholder="<?php echo $date ?>" name="submitdate" id="datesubmitted" hidden="hidden"><br>
<input placeholder="<?php echo $firstname . " " . $lastname ?>" name="submittedby" id="submittedby" hidden="hidden"> 
</form> 
<input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" />
</div>
      
</div>

这是我的 php 提交代码:

include "config.php";

foreach ($_POST["xid"] as $index => $id) {
    
    $appnamesubmit = $_POST['appnamesubmit'];
    $checkname = $_POST['checkname'];
    $status = $_POST['btn'];
    $comment = $_POST['xcomment'];
    $submittedby = $_POST['submittedby'];
    $datesubmit = $_POST['date'];


    $Insert_check = $conn->prepare("INSERT INTO truck_check_log (appname,check_name,status,comment,submitted_by,date) VALUES (?,?,?,?,?,?) ");
    $Insert_check->execute(array($appnamesubmit, $checkname, $status, $comment, $submittedby, $datesubmit));

    if($Insert_check)
    {
        echo"1 record added";
        }
        else
        {
        echo "Error in insertion of the record";
    }
}
php mysql mysqli web-applications
1个回答
0
投票

您有很多地方需要修改

  • $_POST['date'] 应为 $_POST['submitdate'] - 您的提交表单使用该名称
    submitdate
  • $_POST['appnamesubmit']、$_POST['submitdate'] 和 $_POST['subscribedby'] 都不在提交表单的循环内(它们不是数组元素),因此请将它们从 PHP 中移出foreach 循环也
  • 您需要在循环内正确指定数组索引

因此,将您的 PHP 代码更改为以下内容并重试

<?php
include "config.php";

    $appnamesubmit = $_POST['appnamesubmit'];
    $datesubmit = $_POST['submitdate'];
    $submittedby = $_POST['submittedby'];


foreach ($_POST["xid"] as $index => $id) {
    
    $checkname = $_POST['checkname'][$index];
    $status = $_POST['btn'][$id];
    $comment = $_POST['xcomment'][$index];


    $Insert_check = $conn->prepare("INSERT INTO truck_check_log (appname,check_name,status,comment,submitted_by,date) VALUES (?,?,?,?,?,?) ");
    $Insert_check->execute(array($appnamesubmit, $checkname, $status, $comment, $submittedby, $datesubmit));

    if($Insert_check)
    {
        echo"1 record added";
        }
        else
        {
        echo "Error in insertion of the record";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.