插入MySQL表时,PHP Form数组具有空值

时间:2018-11-21 03:01:50

标签: php mysql arrays forms session

我在将php表单数组值插入MySQL表时遇到问题。现在,表单本身从另一个表中提取工作人员信息以填充字段及其值。现在,只有三个工作人员可以填充这些字段,但是随着增加的工作人员,最多可能有40个。当我从表单中仅添加第一个工作人员时,所有信息都会正常插入。但是,当我添加多个时,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";
}

我更改了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";
}

0 个答案:

没有答案