书写一个在线开始系统,并将各文件代码分别发送给我
时间: 2024-01-16 07:02:36 浏览: 31
好的,我可以为您编写一个简单的在线开始系统。这个系统将允许用户注册账户,登陆,创建新的项目并给项目添加任务。以下是各文件代码。
1. `index.php` - 这是项目的主页,用户将在此处注册或登陆账户。
```php
<!DOCTYPE html>
<html>
<head>
<title>Online Starter System</title>
</head>
<body>
<h1>Welcome to Online Starter System</h1>
<p>Please login or register to start your projects.</p>
<p>
<a href="login.php">Login</a> |
<a href="register.php">Register</a>
</p>
</body>
</html>
```
2. `database.php` - 这个文件包含了连接到 MySQL 数据库的代码。您需要将它修改为适合您的数据库配置。
```php
<?php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "online_starter";
$conn = mysqli_connect($db_host, $db_user, $db_password, $db_name);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
```
3. `register.php` - 这是注册账户的页面,当用户填写表单并提交时,它会将数据保存到数据库中。
```php
<?php
include "database.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$confirm_password = $_POST["confirm_password"];
if ($password != $confirm_password) {
echo "Passwords do not match.";
} else {
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
if (mysqli_query($conn, $sql)) {
echo "Registration successful.";
} else {
echo "Error: " . mysqli_error($conn);
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Username:</label>
<input type="text" name="username"><br><br>
<label>Password:</label>
<input type="password" name="password"><br><br>
<label>Confirm Password:</label>
<input type="password" name="confirm_password"><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
```
4. `login.php` - 这是登陆账户的页面,当用户填写表单并提交时,它会将数据与数据库中的数据进行验证。
```php
<?php
include "database.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
echo "Login successful.";
} else {
echo "Invalid username or password.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Username:</label>
<input type="text" name="username"><br><br>
<label>Password:</label>
<input type="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
5. `create_project.php` - 这个页面包含了一个表单,当用户填写表单并提交时,它会在数据库中创建一个新的项目记录。
```php
<?php
include "database.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = $_POST["title"];
$description = $_POST["description"];
$sql = "INSERT INTO projects (title, description) VALUES ('$title', '$description')";
if (mysqli_query($conn, $sql)) {
echo "Project created successfully.";
} else {
echo "Error: " . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Create Project</title>
</head>
<body>
<h1>Create Project</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Title:</label>
<input type="text" name="title"><br><br>
<label>Description:</label>
<textarea name="description"></textarea><br><br>
<input type="submit" value="Create Project">
</form>
</body>
</html>
```
6. `view_projects.php` - 这个页面将从数据库中检索出所有的项目记录,并将它们显示在一个 HTML 表格中。
```php
<?php
include "database.php";
$sql = "SELECT * FROM projects";
$result = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>View Projects</title>
</head>
<body>
<h1>View Projects</h1>
<table>
<tr>
<th>Title</th>
<th>Description</th>
</tr>
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["title"] . "</td>";
echo "<td>" . $row["description"] . "</td>";
echo "</tr>";
}
} else {
echo "No projects found.";
}
?>
</table>
</body>
</html>
```
希望这些代码可以帮到你!