php+mysql实现一个日记管理系统,在这个系统中可以进行日记的添加、修改、删除、浏览、查询等操作,并实现密码保护和密码修改功能。并对日记信息按照日期进行排序,并将日记存储到文件中。代码
时间: 2024-03-25 13:41:40 浏览: 172
php+mysql
好的,下面是一个简单的 PHP + MySQL 日记管理系统的示例代码:
1. 首先是数据库连接代码:
```php
<?php
$servername = "localhost"; // 数据库服务器名
$username = "root"; // 数据库用户名
$password = ""; // 数据库密码
$dbname = "diary"; // 数据库名
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接是否成功
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
```
2. 创建一个表来存储日记信息:
```sql
CREATE TABLE diary (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(30) NOT NULL,
content TEXT NOT NULL,
password VARCHAR(50),
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
```
3. 实现密码加密和解密:
```php
<?php
// 密码加密函数
function encrypt_password($password) {
$salt = "MyDiary"; // 加盐
return md5($password.$salt);
}
// 密码解密函数(因为这里只是简单地对密码进行加密,所以不需要解密函数)
?>
```
4. 实现日记的添加、修改、删除、浏览和查询:
```php
<?php
// 添加日记
function add_diary($title, $content, $password) {
global $conn;
$title = mysqli_real_escape_string($conn, $title); // 防止 SQL 注入
$content = mysqli_real_escape_string($conn, $content);
$password = encrypt_password($password); // 加密密码
$sql = "INSERT INTO diary (title, content, password) VALUES ('$title', '$content', '$password')";
if (mysqli_query($conn, $sql)) {
return true;
} else {
return false;
}
}
// 修改日记
function update_diary($id, $title, $content, $password) {
global $conn;
$title = mysqli_real_escape_string($conn, $title);
$content = mysqli_real_escape_string($conn, $content);
$password = encrypt_password($password);
$sql = "UPDATE diary SET title='$title', content='$content', password='$password' WHERE id=$id";
if (mysqli_query($conn, $sql)) {
return true;
} else {
return false;
}
}
// 删除日记
function delete_diary($id) {
global $conn;
$sql = "DELETE FROM diary WHERE id=$id";
if (mysqli_query($conn, $sql)) {
return true;
} else {
return false;
}
}
// 获取所有日记
function get_all_diaries() {
global $conn;
$sql = "SELECT * FROM diary ORDER BY create_time DESC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$diaries = array();
while($row = mysqli_fetch_assoc($result)) {
$diaries[] = $row;
}
return $diaries;
} else {
return null;
}
}
// 获取一篇日记
function get_diary($id, $password) {
global $conn;
$password = encrypt_password($password);
$sql = "SELECT * FROM diary WHERE id=$id AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
$diary = mysqli_fetch_assoc($result);
return $diary;
} else {
return null;
}
}
// 搜索日记
function search_diaries($keyword) {
global $conn;
$keyword = mysqli_real_escape_string($conn, $keyword);
$sql = "SELECT * FROM diary WHERE title LIKE '%$keyword%' ORDER BY create_time DESC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$diaries = array();
while($row = mysqli_fetch_assoc($result)) {
$diaries[] = $row;
}
return $diaries;
} else {
return null;
}
}
?>
```
5. 实现密码保护和密码修改功能:
```php
<?php
// 检查密码是否正确
function check_password($id, $password) {
global $conn;
$password = encrypt_password($password);
$sql = "SELECT * FROM diary WHERE id=$id AND password='$password'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1) {
return true;
} else {
return false;
}
}
// 修改密码
function change_password($id, $old_password, $new_password) {
global $conn;
$old_password = encrypt_password($old_password);
$new_password = encrypt_password($new_password);
if (check_password($id, $old_password)) {
$sql = "UPDATE diary SET password='$new_password' WHERE id=$id";
if (mysqli_query($conn, $sql)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
?>
```
6. 最后是一个简单的前端页面代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>My Diary</title>
</head>
<body>
<h1>My Diary</h1>
<hr>
<h2>Add a new diary</h2>
<form action="add.php" method="post">
Title: <input type="text" name="title"><br>
Content: <br>
<textarea name="content" rows="10" cols="50"></textarea><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Add">
</form>
<hr>
<h2>All diaries</h2>
<?php
include 'functions.php';
$diaries = get_all_diaries();
if ($diaries == null) {
echo "No diaries found.";
} else {
foreach ($diaries as $diary) {
$id = $diary['id'];
$title = $diary['title'];
$content = $diary['content'];
$create_time = $diary['create_time'];
echo "<h3><a href='view.php?id=$id'>$title</a></h3>";
echo "<p>$content</p>";
echo "<p>Created time: $create_time</p>";
echo "<hr>";
}
}
?>
</body>
</html>
```
这只是一个简单的示例代码,实际上还有很多细节需要注意,比如权限控制、防止 SQL 注入、前端验证等等。
阅读全文