为什么它插入额外的两个空行

时间:2016-12-21 13:30:42

标签: php mysql

我正在尝试插入多行,当我插入数据时,需要额外的两行空行。但为什么会发生......请有人帮我解决这个问题......... 这是我的表单页面

<?php
require_once 'Insert_class.php';
$obj_Insert_class=new Insert_class();
$new= $obj_Insert_class->employee_select_data();
$massage='';
if(isset($_POST['btn'])){
    $massage=$obj_Insert_class->time_add();
}
?>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
    <center>
        <h2>Multiple Insert</h2>
        <?php  echo $massage;   ?>
        <form action="" method="post">
            <table width='30%'>
                <tr>
                    <td><u><b>EMPLOYEE NAME</b></u></td>
                </tr>
                <?php
                while ($row = mysqli_fetch_assoc($new)) {
                    ?>
                    <tr>
                        <td><input name="eid[]" class="case" type="text" value="<?php echo $row['eid']; ?>" /></td>
                        <td><input name="employee_name[]" class="case" type="text" value="<?php echo $row['employee_name'] ?>"/></td>
                        <td><input name="status[]" class="case" type="text" value="1"/></td>
                        <td><input name="first[]" id="first" type="number"/></td>
                        <td><input name="second[]" id="second" type="number"/></td>
                    </tr>
                <?php } ?>
                <tr>
                    <td></td>
                    <td><input type="submit" name="btn" value="Submit" /></td>
                </tr>
            </table>
        </form>     
    </center>

</body>
</html>

这是我的插入课程页面

<?php

require './connect.php';

class Insert_class extends Connect {

    protected $link;

    public function __construct() {
        $this->link = $this->conntection();
    }

    public function time_add() {
        $i = 0;
        foreach ($_POST as $val) {
            error_reporting(0);
            $eid = $_POST['eid'][$i];
            $name = $_POST['employee_name'][$i];
            $status = $_POST['status'][$i];
            $first = $_POST['first'][$i];
            $second = $_POST['second'][$i];
            $new_add = $first + $second;
            $SQL = "INSERT INTO time(eid,name,status,first,seconc,new_add)VALUES('$eid','$name','$status','$first','$second','$new_add')";
            if (mysqli_query($this->link, $SQL)) {
                $massage = 'DATA Susseccfully inserted';
            } else {
                die('Select query problem' . mysqli_error($this->link));
            }
            $i++;
        }
        return $massage;

    }

    public function employee_select_data() {
        $SQL = "SELECT * FROM employee";

        if (mysqli_query($this->link, $SQL)) {
            $result = mysqli_query($this->link, $SQL);
            return $result;
        } else {
            die('Select query problem' . mysqli_error($this->link));
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您的代码包含此循环。

   $i = 0;
   foreach ($_POST as $val) {  /* wrong! */
        /* do the insert */
        $i ++;
   }

这没有任何意义。 $_POST是从表单发布的值数组。此循环遍历该数组中的每个值。这种迭代太多了。

相反,您需要在$_POST的表单字段元素中找出数组的大小。尝试这样的事情:

$some_field = $_POST['eid'];
$i = 0;
foreach ($some_field as $junk) {
    /* do the insert */
    $i++;
}