PHP CRUD 加载空白页或显示错误未定义数组键
PHP CRUD Loading blank page or shows error undefined array key
我正在做一个大学项目,所以目前不需要特别安全。
我对 PHP 的了解不是最好的,我目前正在努力让自己了解更多。
我已经设法让一些 CRUD 功能正常工作,例如创建和更新,但我的删除让我有些头疼。
我希望登录用户能够通过单击删除按钮从数据库中删除一行,但目前这只会加载一个空白的白页,没有错误,如果我将 $_POST 更改为 $_GET,然后错误显示未定义的数组键。
在这一点上,老实说,我不确定还能做什么,任何建议或帮助将不胜感激。
这是主管理主页。
<?php require('components/header.inc.php'); ?>
<section class="container-md">
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="mt-5 mb-3 clearfix">
<h2 class="pull-left">Dashboard</h2>
<a href="create.php" class="btn btn-success pull-right"><i class="fa fa-plus"></i> Add New Course</a>
</div>
<table class="table table-bordered table-striped">
<thead>
<?php include './rename.php'; ?>
</table>
</div>
</div>
</div>
</div>
这个显示数据库。
<?php include './components/connnlceducation.php';
echo "<thead>";
echo "<tr>";
echo "<th>Course Level</th>";
echo "<th>Course Name</th>";
echo "<th>Course Type</th>";
echo "<th>Start Date</th>";
echo "<th>View More</th>";
echo "</tr>";
echo "</thead>";
$sql = "SELECT * FROM courselist";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "<tbody>";
echo "<tr>";
if ($row['courseid']) {
echo '<form class="form-inline m-2" action="./update.php" method="POST">';
echo '<td><input type="text" class="form-control" name="courselevel" value="' .
$row['courselevel'] . '"></td>';
echo '<td><input type="text" class="form-control" name="coursename" value="' .
$row['coursename'] . '"></td>';
echo '<td><input type="text" class="form-control" name="coursetype" value="' .
$row['coursetype'] . '"></td>';
echo '<td><input type="text" class="form-control" name="startdate" value="' .
$row['startdate'] . '"></td>';
echo '<td><input type="text" class="form-control" name="viewmore" value="' .
$row['viewmore'] . '"></td>';
echo '<td><button type="submit" class="btn btn-primary">Save</button></td>';
echo '<td><a class="btn btn-danger" name="delete" href="./delete.php?id=' .
$row['courseid'] . '" role="button">Delete</a></td>';
echo '<input type="hidden" name="courseid" value="' . $row['courseid'] . '">';
echo '</form>';
}
echo "</tr>";
echo "</tbody>";
}
$conn->close();
?>
这是我的更新代码,效果很好。
<?php
include './components/connnlceducation.php';
$id = $_POST['courseid'];
$courselevel = $_POST['courselevel'];
$coursename = $_POST['coursename'];
$coursetype = $_POST['coursetype'];
$startdate = $_POST['startdate'];
$viewmore = $_POST['viewmore'];
$sql = "UPDATE courselist SET courselevel='$courselevel',
coursename='$coursename',
coursetype='$coursetype',
startdate='$startdate',
viewmore='$viewmore'
WHERE courseid=$id";
$result = $conn->query($sql);
$conn->close();
header("location: ./adminhome.php");
?>
这是问题代码。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include './components/connnlceducation.php';
if (isset($_POST['delete'])) {
$id = $_GET['delete'];
$sql = "DELETE FROM courselist WHERE courseid=$id";
$result = $conn->query($sql);
$conn->close();
header("location: ./adminhome.php");
}
更改代码如下:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include './components/connnlceducation.php';
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "DELETE FROM courselist WHERE courseid=$id";
$result = $conn->query($sql);
$conn->close();
header("location: ./adminhome.php");
}
我正在做一个大学项目,所以目前不需要特别安全。 我对 PHP 的了解不是最好的,我目前正在努力让自己了解更多。
我已经设法让一些 CRUD 功能正常工作,例如创建和更新,但我的删除让我有些头疼。
我希望登录用户能够通过单击删除按钮从数据库中删除一行,但目前这只会加载一个空白的白页,没有错误,如果我将 $_POST 更改为 $_GET,然后错误显示未定义的数组键。
在这一点上,老实说,我不确定还能做什么,任何建议或帮助将不胜感激。 这是主管理主页。
<?php require('components/header.inc.php'); ?>
<section class="container-md">
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="mt-5 mb-3 clearfix">
<h2 class="pull-left">Dashboard</h2>
<a href="create.php" class="btn btn-success pull-right"><i class="fa fa-plus"></i> Add New Course</a>
</div>
<table class="table table-bordered table-striped">
<thead>
<?php include './rename.php'; ?>
</table>
</div>
</div>
</div>
</div>
这个显示数据库。
<?php include './components/connnlceducation.php';
echo "<thead>";
echo "<tr>";
echo "<th>Course Level</th>";
echo "<th>Course Name</th>";
echo "<th>Course Type</th>";
echo "<th>Start Date</th>";
echo "<th>View More</th>";
echo "</tr>";
echo "</thead>";
$sql = "SELECT * FROM courselist";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "<tbody>";
echo "<tr>";
if ($row['courseid']) {
echo '<form class="form-inline m-2" action="./update.php" method="POST">';
echo '<td><input type="text" class="form-control" name="courselevel" value="' .
$row['courselevel'] . '"></td>';
echo '<td><input type="text" class="form-control" name="coursename" value="' .
$row['coursename'] . '"></td>';
echo '<td><input type="text" class="form-control" name="coursetype" value="' .
$row['coursetype'] . '"></td>';
echo '<td><input type="text" class="form-control" name="startdate" value="' .
$row['startdate'] . '"></td>';
echo '<td><input type="text" class="form-control" name="viewmore" value="' .
$row['viewmore'] . '"></td>';
echo '<td><button type="submit" class="btn btn-primary">Save</button></td>';
echo '<td><a class="btn btn-danger" name="delete" href="./delete.php?id=' .
$row['courseid'] . '" role="button">Delete</a></td>';
echo '<input type="hidden" name="courseid" value="' . $row['courseid'] . '">';
echo '</form>';
}
echo "</tr>";
echo "</tbody>";
}
$conn->close();
?>
这是我的更新代码,效果很好。
<?php
include './components/connnlceducation.php';
$id = $_POST['courseid'];
$courselevel = $_POST['courselevel'];
$coursename = $_POST['coursename'];
$coursetype = $_POST['coursetype'];
$startdate = $_POST['startdate'];
$viewmore = $_POST['viewmore'];
$sql = "UPDATE courselist SET courselevel='$courselevel',
coursename='$coursename',
coursetype='$coursetype',
startdate='$startdate',
viewmore='$viewmore'
WHERE courseid=$id";
$result = $conn->query($sql);
$conn->close();
header("location: ./adminhome.php");
?>
这是问题代码。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include './components/connnlceducation.php';
if (isset($_POST['delete'])) {
$id = $_GET['delete'];
$sql = "DELETE FROM courselist WHERE courseid=$id";
$result = $conn->query($sql);
$conn->close();
header("location: ./adminhome.php");
}
更改代码如下:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include './components/connnlceducation.php';
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "DELETE FROM courselist WHERE courseid=$id";
$result = $conn->query($sql);
$conn->close();
header("location: ./adminhome.php");
}