学生信息管理系统php代码
Below is a simple example of a student information management system implemented in PHP. This system allows for adding, editing, deleting, and viewing student records stored in a MySQL database. Before using this code, make sure you have set up a MySQL database with a table named students
with appropriate fields (e.g., id
, name
, email
, phone
).
php<!DOCTYPE html>
<html>
<head>
<title>Student Information Management System</title>
</head>
<body>
<h2>Student Information Management System</h2>
<a href="add_student.php">Add New Student</a>
<br><br>
<?php
// Database connection
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Fetching data
$sql = "SELECT * FROM students";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>Email</th><th>Phone</th><th>Action</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['phone']."</td>";
echo "<td><a href='edit_student.php?id=".$row['id']."'>Edit</a> | <a href='delete_student.php?id=".$row['id']."'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</body>
</html>
This code displays a list of students with options to edit or delete each student. You'll also need to create add_student.php
, edit_student.php
, and delete_student.php
files for adding, editing, and deleting student records respectively. These files will handle form submissions and database interactions for adding, editing, and deleting records.
Sure, let's continue by providing the code for adding a new student (add_student.php
), editing an existing student (edit_student.php
), and deleting a student (delete_student.php
).
add_student.php
php<!DOCTYPE html>
<html>
<head>
<title>Add New Student</title>
</head>
<body>
<h2>Add New Student</h2>
<form action="save_student.php" method="post">
<label>Name:</label><br>
<input type="text" name="name" required><br>
<label>Email:</label><br>
<input type="email" name="email" required><br>
<label>Phone:</label><br>
<input type="text" name="phone" required><br>
<input type="submit" value="Save">
</form>
</body>
</html>
edit_student.php
php<?php
// Database connection
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "SELECT * FROM students WHERE id=$id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
} else {
echo "No student ID provided.";
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Student</title>
</head>
<body>
<h2>Edit Student</h2>
<form action="save_student.php" method="post">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<label>Name:</label><br>
<input type="text" name="name" value="<?php echo $row['name']; ?>" required><br>
<label>Email:</label><br>
<input type="email" name="email" value="<?php echo $row['email']; ?>" required><br>
<label>Phone:</label><br>
<input type="text" name="phone" value="<?php echo $row['phone']; ?>" required><br>
<input type="submit" value="Update">
</form>
</body>
</html>
<?php
mysqli_close($conn);
?>
delete_student.php
php<?php
// Database connection
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "DELETE FROM students WHERE id=$id";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
} else {
echo "No student ID provided.";
}
mysqli_close($conn);
?>
In these PHP files, replace "localhost"
, "username"
, "password"
, and "database"
with your actual MySQL server details. Make sure you have proper error handling and security measures in place before deploying this code in a production environment.