我正在尝试提交一份用于消防部门设备日常检查的表格。该表单是一个 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>		
<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>		
<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>		
<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";
}
}
您有很多地方需要修改
submitdate
因此,将您的 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";
}
}