Foreach循环错误地在一列中发布值

时间:2014-02-11 09:46:37

标签: php mysql

请帮帮我。我开发了一个考勤记录,通过表格捕获数据。用户完成后,所有值都将发布到mysql。这是表格:

<form action="processAttRegister.php" method="post" name="frmAttregister">
<table width="96%" border="1" align ="center" cellpadding="0" cellspacing="0" id="projectOverview">
    <tr>
        <th width="72">Date</th>
        <th width="156">Name</th>
        <th width="50">Present</th>
        <th width="152">Reason For Absence</th>
        <th width="44">Time In</th>
        <th width="46">Time Out</th>
        <th width="107">Remarks</th>
        <?php    
            // we'll increase this new variable by 1 on each loop,
            // to make sure it's always unique
            $count = 0; 

            $atts = mysql_query("SELECT * FROM tblemployeedata WHERE grade>4 AND section=3");
            while($att = mysql_fetch_array($atts)) {

            // increase the count variable by 1
            $count++;

            // $count is added inside each ID attribute below to ensure uniqueness.
        ?>
        <tr id="empattlist">
            <td><input name="date[]" id="date<?php echo $count; ?>" type="text" value="<?php echo date("Y-m-d");?>" size="12"/></td>
            <td><input name="name[]" id="name<?php echo $count; ?>" type="text" value="<?php echo $att["empID"];?>" readonly hidden /><?php echo $att["firstname"]." ".$att["surname"]."(".$att["empID"].")";?></td>
            <td>
                <div align="center">
                    <input name="emppresent[]" id="emppresent<?php echo $count; ?>" type="checkbox" value="Yes" onclick="hideShowElements(<?php echo $count; ?>)"/>
                    <input type="hidden" name="emppresent[]" id="emppresent<?php echo $count; ?>" value="No"/>
                </div>
            </td>
            <td id="abs_reason">
                <select name="absentReason[]" id="absentReason<?php echo $count; ?>">
                    <option value="" selected="selected">-</option>
                    <option value="Other">Other</option>
                    <option value="Funeral">Attending Funeral</option>
                    <option value="Sick">Sick</option>
                </select>

            </td>
            <td><input type="text" name="timein[]" id="timein<?php echo $count; ?>" size="7" /></td>
            <td><input type="text" name="timeout[]" id="timeout<?php echo $count; ?>" size="7"/></td>
            <td>
            <textarea name="remarks[]" cols="15" rows="1" id="remarks<?php echo $count; ?>" ></textarea>
            </td>
        </tr>
        <?php
            // pass count to the function, also on the other function call below.
            echo '<script type="text/javascript">initiallyHideElements(' . $count . ');</script>';
            }

        ?>
        <tr>
            <td colspan="7">
            <div align="center">
            <input name="postAttendance" type="submit" id="postAttendance" value="Submit" align="center" />

            </div>
            </td>
        </tr>
</table>

以下是用于在mysql中向表中插入值的代码。

如果(isset($ _ POST [ 'postAttendance'])){

    foreach($_POST["date"] as $k) 
    {
        if(!empty($v)) 
            {
              $attendaceData = "INSERT INTO  tblattendanceregister (date, employee,present,absentReason,timeIn,timeOut,remarks) 
                            VALUE ('".$k."', '".$_POST['name']."', '".$_POST['emppresent']."', '".$_POST['absentReason']."',
                                    '".$_POST['timein']."', '".$_POST['timeout']."', '".$_POST['remarks']."')";
              $post=mysql_query($attendaceData);
            }
    }

    if($post){
        header("Location: AttendanceRegister.php");
    }
    else
    {
        $error=mysql_error();
        header("Location: AttendanceRegister.php?post=error&problem=$error");
    }
}

发布操作很好,但“现在”中的值被错误地发布。只有前三个记录才会正确发布。

0 个答案:

没有答案