警告:试图访问C:xampphtdocscrudread.php第41行中null类型值的数组偏移量



我想创建一个网站,可以做基本的我的SQL功能,如创建,读取,更新和删除,我在阅读页面工作时,我得到了这个错误:

警告:试图访问C:xampphtdocscrudread.php第41行中null类型值的数组偏移。

下面是我的代码:
<?php
$link = mysqli_connect("localhost", "root", "", "db_school");
if (mysqli_connect_error()) {
die ("Database Connection Error");
}
$query = "SELECT * FROM tbl_student ORDER BY id DESC";
$result = mysqli_query($link, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Read</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
</head>
<body>
<?php
include 'procces_read.php';
?>
<div class="container">
<div class="box">
<h4 class="display-4 text-center">Read</h4>
<br>
<?php if (isset($_GET['success'])) { ?>
<div class="alert alert-success" role="alert">
<?php echo $_GET['success']; ?>
</div>
<?php } ?>
<?php if (mysqli_num_rows($result)) { ?>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Student ID</th>
<th scope="col">Student Name</th>
<th scope="col">Student DOB</th>
<th scope="col">Student Sex</th>
<th scope="col">Student Class</th>
</tr>
</thead>
<tbody>
<?php 
$i = 0;
while($rows = mysqli_fetch_assoc($result)){
$i++;
}
?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $rows['student_id'];?></td>
<td><?php echo $rows['Name']; ?></td>                       
<td><?php echo $rows['DOB']; ?></td>                        
<td><?php echo $rows['sex']; ?></td>                        
<td><?php echo $rows['Class']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="link-right">
<a href="create.php" class="link-primary">Create</a>
</div>
</div>
</div>
</body>
</html>

您的表行(从<tr></tr>)应该在while循环内。$rows变量没有在它之外定义。

<tbody>
<?php 
$i = 0;
while($rows = mysqli_fetch_assoc($result)){
$i++;
?>
<tr>
<th scope="row"><?php echo $i; ?></th>
<td><?php echo $rows['student_id'];?></td>
<td><?php echo $rows['Name']; ?></td>                       
<td><?php echo $rows['DOB']; ?></td>                        
<td><?php echo $rows['sex']; ?></td>                        
<td><?php echo $rows['Class']; ?></td>
</tr>
<?php }
} ?>

相关内容

最新更新