php表单数组在插入mysql表时有空值

bqucvtff  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(310)

我在将php表单数组值插入mysql表时遇到了一个问题。现在,表单本身从另一个表中提取工作者信息来填充字段及其值。现在只有三个工人会填充这些字段,但是随着更多工人的加入,可能会有多达40个工人。当我只添加表单中的第一个worker时,所有信息都会正常插入。但是,当我添加多个时,title和employeeid字段是空的,我不知道为什么。任何帮助都将不胜感激。
以下是表格:

<form method = 'POST' action = 'addworkers.php'>
    <?php
        $sql2    = "select * from workers where companyId = 1";
        $result2 = mysqli_query($conn,$sql2);
        $numRows = mysqli_num_rows($result2);
        $check   = 0;
        while($row2 = $result2->fetch_assoc()) {
            $employeeId = $row2["id"];
            $name = $row2["name"];
            echo '<input type="checkbox" name="employeeId[]" value="' . $employeeId . '">';
            echo "$name\n";
            echo '<input type = "hidden" value="' . $companyId . '" name = "companyId[]"/>';
            echo '<input type = "hidden" value="' . $jobNumber . '" name = "jobNumber[]"/>';                
            echo 'Site Title : <input type = "text" name = "title[]"/><br/>';
        }
    ?>
    <input type="hidden" name="count" value="<?php echo "$numRows"; ?>"/>
    <input type = 'submit' value = 'SEND'/>
</form>

然后是addworkers.php代码

require_once("dbConfig.php");
session_start();
$timestamp = date("Y-m-d");

if (isset($_SESSION['loginname'])) {
    $companyId  = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
    $jobNumber  = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
    $employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
    $title      = isset($_POST['title']) ? $_POST['title'] : "" ;

    foreach($title as $key=>$value){
        if (!empty($value)) {
            $query = "insert into `Jobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId[$key]', '$jobNumber[$key]','$employeeId[$key]','$value','$timestamp')";
            $result = mysqli_query($conn,$query);
        }
    }
} else {
    echo "Error";
}

我改变了回声“错误”行,但输出是明确的。
这一定是一个数组如何计数的问题,但我不知道如何修复它。在表单中,如果我选中每行旁边的复选框,所有信息都会正确地输入到表中。如果我只检查第二行和/或第三行,它不包括网站标题,并且employeeid是反向的。
以下是表单的输出:

<form method = 'POST' action = 'insertworkers.php'>
    <input type="checkbox" name="employeeId[]" value="1">Mike
    <input type = "hidden" value="1" name = "companyId"/>
    <input type = "hidden" value="12345" name = "jobNumber"/>
    Site Title : <input type = "text" name = "title[]"/>

    <input type="checkbox" name="employeeId[]" value="2">Steve
    <input type = "hidden" value="1" name = "companyId"/>
    <input type = "hidden" value="12345" name = "jobNumber"/>
    Site Title : <input type = "text" name = "title[]"/>

    <input type="checkbox" name="employeeId[]" value="3">Roger
    <input type = "hidden" value="1" name = "companyId"/>
    <input type = "hidden" value="12345" name = "jobNumber"/>
    Site Title : <input type = "text" name = "title[]"/>

    <input type="hidden" name="count" value="3"/>
    <input type = 'submit' value = 'SEND'/>
 </form>

我还将foreach循环更改为for循环,因为数组深度应该相同,因为所有字段都是必需的

require_once("dbConfig.php");
session_start();
$counter   = "".$_POST["count"]."";
$timestamp = date("Y-m-d");

if ( isset( $_SESSION['loginname'] ) ) {
    $companyId  = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
    $jobNumber  = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
    $employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
    $title      = isset($_POST['title']) ? $_POST['title'] : "" ;

    for($i=0, $count = count($employeeId);$i<$count;$i++){
        if (!empty($employeeId)) {
            $query = "insert into `customerJobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId', '$jobNumber','$employeeId[$i]','$title[$i]','$timestamp')";
            $result = mysqli_query($conn,$query);
        }
    }
} else {
    echo "Error";
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题