您好我正在尝试在mysql中插入多个html表行时收到未定义的偏移量:2通知。这是我的html表单代码 `
<form action="insert_array.php" method="post">
<table class="table table-bordered">
<thead>
<tr>
<th>Name of the degree/certificates</th>
<th>Name of the Institution</th>
<th>Group</th>
<th>Board</th>
<th>Year</th>
<th>Grade/GPA</th>
</tr>
</thead>
<tr>
<td>SSC</td>
<td><input name="institution[]" type="text" class="form-control" ></td>
<td><input name="subjectGroup[]" type="text" class="form-control" ></td>
<td><input name="board[]" type="text" class="form-control" ></td>
<td><input name="year[]" type="text" class="form-control" ></td>
<td><input name="grade[]" type="text" class="form-control" ></td>
</tr>
<tr>
<td>HSC</td>
<td><input name="institution[]" type="text" class="form-control" ></td>
<td><input name="subjectGroup[]" type="text" class="form-control" ></td>
<td><input name="board[]" type="text" class="form-control" ></td>
<td><input name="year[]" type="text" class="form-control" ></td>
<td><input name="grade[]" type="text" class="form-control" ></td>
</tr>
</table>
<input type="submit" name="submit" value="submit">
</form>
` 这是我的PHP代码
if($conn === false){
die("Error: Could not connect. ".mysqli_connect_error());
}
if(isset($_POST['submit'])){
$i = 0;
foreach ($_POST as $val){
$institution = $_POST['institution'][$i];
$board = $_POST['board'][$i];
$year = $_POST['year'][$i];
$subjectGroup = $_POST['subjectGroup'][$i];
$grades = $_POST['grade'][$i];
mysqli_query($conn,"INSERT INTO ems_student_qualification (institution,board,year,subjectGroup,grade) VALUES ('$institution', '$board','$year','$subjectGroup','$grades')");
$i++;
}
}
&GT?; 我无法在我的数据库中存储值。但是,当我只用2个字段尝试这个过程时,它工作正常。请指出我的错误是什么?并提前感谢。
答案 0 :(得分:1)
您正在循环$_POST
而不是多少项
if(isset($_POST['submit'])){
foreach ($_POST['institution'] as $i => $val){
$institution = $_POST['institution'][$i];
$board = $_POST['board'][$i];
$year = $_POST['year'][$i];
$subjectGroup = $_POST['subjectGroup'][$i];
$grades = $_POST['grade'][$i];
mysqli_query($conn,"INSERT INTO ems_student_qualification (institution,board,year,subjectGroup,grade) VALUES ('$institution', '$board','$year','$subjectGroup','$grades')");
}
}
答案 1 :(得分:1)
尝试使用PHP代码:
if($conn === false){
die("Error: Could not connect. ".mysqli_connect_error());
}
if(isset($_POST['submit'])){
$sValues = 'INSERT INTO ems_student_qualification (´institution´, ´board´, ´year´, ´subjectGroup´, ´grade´) VALUES ';
$i = 0;
for ($count = count($_POST['institution']); $i < $count; $i++){
if ( $i > 1 ) { $sValues .= ', '; }
$institution = mysqli_real_escape_string($conn, $_POST['institution'][$i]);
$board = mysqli_real_escape_string($conn, $_POST['board'][$i]);
$year = mysqli_real_escape_string($conn, $_POST['year'][$i]);
$subjectGroup = mysqli_real_escape_string($conn, $_POST['subjectGroup'][$i]);
$grades = mysqli_real_escape_string($conn, $_POST['grade'][$i]);
$sValues .= '(´' . $institution . '´, ´' . $board . '´, ´' . $year . '´, ´' . $subjectGroup . '´, ´' . $grades . '´)';
}
if ( $i > 0 ) {
mysqli_query($conn, $sValues);
}
}
它还包括字符串转义。
答案 2 :(得分:0)
if(isset($_POST['submit'])){
for ($i=0; $i < count($_POST['institution']); $i++ )
{
$institution = $_POST['institution'][$i];
$board = $_POST['board'][$i];
$year = $_POST['year'][$i];
$subjectGroup = $_POST['subjectGroup'][$i];
$grades = $_POST['grade'][$i];
mysqli_query($conn,"INSERT INTO ems_student_qualification (institution,board,year,subjectGroup,grade) VALUES ('$institution', '$board','$year','$subjectGroup','$grades')");
}}
答案 3 :(得分:0)
你的循环不合适,因为它减少了总变量的数量,即11次,但是$ _POST [&#39;机构&#39;]只存在2次(数组从零开始)所以第3次它显示{{ 1}}。
可以使用undefined error
或任何输入字段值进行循环,例如:$_POST['institution']
,也可以使用for循环。希望这很清楚。