gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:07:10 27 4
gpt4 key购买 nike

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

<?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));
}
}

}

最佳答案

您的代码包含此循环。

   $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++;
}

关于php - 为什么它插入额外的两个空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41263968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com