创建数据库:使用MySQL或其他关系型数据库创建一个名为cinema的数据库。 创建表:在cinema数据库中创建user和movie两个表。user表用于存储用户信息,movie表用于存储电影信息。 注册页面:创建一个register.php页面,包含用户名、密码、确认密码等表单项。将表单数据插入到user表中。 登录页面:创建一个login.php页面,包含用户名、密码等表单项。查询user表中是否存在该用户。 管理页面:创建一个admin.php页面,用于管理电影信息。在该页面中,可以添加、删除、修改电影信息。将电影信息插入到movie表中。 显示页面:创建一个show.php页面,用于展示电影信息。查询movie表中的电影信息,并将其展示在页面上。
时间: 2024-02-18 13:59:41 浏览: 85
好的,以下是一个简单的电影院注册登录管理页面的PHP代码实现:
1. 创建数据库和表:
```
CREATE DATABASE cinema;
USE cinema;
CREATE TABLE user (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL
);
CREATE TABLE movie (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(30) NOT NULL,
director VARCHAR(30) NOT NULL,
release_date DATE
);
```
2. 注册页面(register.php):
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h2>Register</h2>
<form action="register.php" method="post">
<label>Username:</label><br>
<input type="text" name="username"><br>
<label>Password:</label><br>
<input type="password" name="password"><br>
<label>Confirm Password:</label><br>
<input type="password" name="confirm_password"><br>
<input type="submit" value="Register">
</form>
<?php
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "cinema");
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$confirm_password = $_POST["confirm_password"];
// Check if passwords match
if ($password != $confirm_password) {
echo "Passwords do not match.";
} else {
// Insert user into database
$sql = "INSERT INTO user (username, password) VALUES ('$username', '$password')";
if (mysqli_query($conn, $sql)) {
echo "Registration successful.";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
// Close database connection
mysqli_close($conn);
?>
</body>
</html>
```
3. 登录页面(login.php):
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="login.php" method="post">
<label>Username:</label><br>
<input type="text" name="username"><br>
<label>Password:</label><br>
<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
<?php
// Start session
session_start();
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "cinema");
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
// Check if user exists in database
$sql = "SELECT * FROM user WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
// User exists, set session variables and redirect to admin page
$_SESSION["username"] = $username;
header("Location: admin.php");
} else {
// User does not exist, display error message
echo "Invalid username or password.";
}
}
// Close database connection
mysqli_close($conn);
?>
</body>
</html>
```
4. 管理页面(admin.php):
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Admin</title>
</head>
<body>
<?php
// Start session
session_start();
// Check if user is logged in
if (!isset($_SESSION["username"])) {
header("Location: login.php");
}
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "cinema");
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = $_POST["title"];
$director = $_POST["director"];
$release_date = $_POST["release_date"];
// Insert movie into database
$sql = "INSERT INTO movie (title, director, release_date) VALUES ('$title', '$director', '$release_date')";
if (mysqli_query($conn, $sql)) {
echo "Movie added successfully.";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
// Retrieve movies from database
$sql = "SELECT * FROM movie";
$result = mysqli_query($conn, $sql);
// Display movies in table
echo "<h2>Admin</h2>";
echo "<form action='admin.php' method='post'>";
echo "<label>Title:</label><br>";
echo "<input type='text' name='title'><br>";
echo "<label>Director:</label><br>";
echo "<input type='text' name='director'><br>";
echo "<label>Release Date:</label><br>";
echo "<input type='date' name='release_date'><br>";
echo "<input type='submit' value='Add Movie'>";
echo "</form>";
echo "<br>";
echo "<table>";
echo "<tr><th>Title</th><th>Director</th><th>Release Date</th><th></th><th></th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["title"] . "</td>";
echo "<td>" . $row["director"] . "</td>";
echo "<td>" . $row["release_date"] . "</td>";
echo "<td><a href='delete.php?id=" . $row["id"] . "'>Delete</a></td>";
echo "<td><a href='edit.php?id=" . $row["id"] . "'>Edit</a></td>";
echo "</tr>";
}
echo "</table>";
// Close database connection
mysqli_close($conn);
?>
<br>
<a href="logout.php">Logout</a>
</body>
</html>
```
5. 显示页面(show.php):
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Show</title>
</head>
<body>
<h2>Show</h2>
<?php
// Connect to database
$conn = mysqli_connect("localhost", "username", "password", "cinema");
// Retrieve movies from database
$sql = "SELECT * FROM movie";
$result = mysqli_query($conn, $sql);
// Display movies in table
echo "<table>";
echo "<tr><th>Title</th><th>Director</th><th>Release Date</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["title"] . "</td>";
echo "<td>" . $row["director"] . "</td>";
echo "<td>" . $row["release_date"] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close database connection
mysqli_close($conn);
?>
</body>
</html>
```
以上代码仅供参考,具体实现还需要根据具体需求进行调整。同时,使用PHP开发需要注意安全性问题,如SQL注入等。
阅读全文