php+sql日记本

发布于 16 天前  85 次阅读


Mysql日记本

背景:上一章我记录了插入mysql的代码,也发现了太多坑了,今天正好有空,就把读取完善了吧,并且我想把这个项目做成日记本,多敲敲代码,练手,用php读取比录入简单多了,就让我现在来开始(乱搞)吧!上读取代码:

$sql = "SELECT name, txt FROM Mytext";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// 输出数据
while($row = mysqli_fetch_assoc($result)) {
echo "标题: " . $row["name"]. " - 内容: " . $row["txt"]."<br>";
}
} else {
echo "没有东西,快去添加吧!";
}

现在,我们完成了最基本的功能,样式我决定用我主题里的微语模块,确实很好看,那么现在就来美化吧!

插入样式:

<link rel='stylesheet' href='https://www.lihouse.xyz/wp-content/themes/Scilper/style.css?ver=2.0.4type='text/css'>
然后把上面打印的部分加上 微语 样式的div,原本的是这样的:

实例-这是一个框框,但是怪好看的,不是嘛


是不是很漂亮?那我们开始着手添加吧!

代码:

echo "<article class='post post-list'><div class='post-status'><div class='postava'><a><img class='avatar avatar-64 photo' src='https://www.lihouse.xyz/wp-content/uploads/2020/05/favicon.png' alt='img' width='64' height='64' /></a></div><div class='s-content'>标题: " . $row["name"]. " - 内容: " . $row["txt"]."</div><hr /></div></article>";

大功告成!
现在我们把输入框的action设置属性到读取
全部代码在这里:
index.php:

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel='stylesheet' type='text/css' href='https://www.lihouse.xyz/wp-content/themes/Scilper/style.css?ver=2.0.4'>

<title>提交一言</title>

<body>
<div class="pattern-center ">
<div class="pattern-attachment-img" style="background-image: url(bing.php)"> </div>
<header class="pattern-header "><h1 class="entry-title">我的日记本</h1></header>
</div>
<div id="phone" style="
"><br></br>
<div style="text-align: center;">
<form action="/post.php" method="post" name="loghitoke">标题:<input name="hitoke" type="text" value="" />内容:<input name="nr" type="text" value="" />  <input type="submit" value="提交" /></form></div>
</div>

<?php
$servername = "localhost";
$username = "你的数据库名称";
$password = "数据库密码";
$dbname = "数据库名";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$sql = "SELECT name, txt FROM Mytext";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// 输出数据
while($row = mysqli_fetch_assoc($result)) {
echo "<article class='post post-list'><div class='post-status'><div class='postava'><a><img class='avatar avatar-64 photo' src='https://www.lihouse.xyz/wp-content/uploads/2020/05/favicon.png' alt='img' width='64' height='64' /></a></div><div class='s-content'>标题: " . $row["name"]. " - 内容: " . $row["txt"]."</div><hr /></div></article>";}
} else {
echo "木有东西唉,快去写一个吧!~";
}
mysqli_close($conn);
?>

好哒,index.php也就是主页写完啦!
我们现在把触发用到的post.php写好吧
post.php:

<?php
$servername = "localhost";
$username = "XXXX";
$password = "XXXXX";
$dbname = "懒得打X了,记得更换";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$sql = "CREATE TABLE Mytext (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30),
txt VARCHAR(1500)
)";

if (mysqli_query($conn, $sql)) {
echo "数据表创建成功";
} else {
/*echo "如果无法达到效果请把这一行的注释去掉,按照报错改: " . mysqli_error($conn);*/
}
$sql = "INSERT INTO Mytext (name, txt) VALUES `{$_POST['hitoke']}`, `{$_POST['nr']}`";
$sql = "INSERT INTO Mytext (name, txt)
VALUES (\"{$_POST['hitoke']}\", \"{$_POST['nr']}\")";
if (mysqli_query($conn, $sql)) {
echo "ok";
echo "<script>alert('添加成功!请点击确定且刷新index.php');history.go(-1)</script>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);

好了,只剩下Bing每日一图了:

bing.php:

<?php
$str = file_get_contents('http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1'); // 从bing获取数据

if(preg_match('/<url>([^<]+)<\/url>/isU', $str, $matches)) { // 正则匹配抓取图片url
$imgurl = 'http://cn.bing.com'.$matches[1];
} else { // 如果由于某些原因,没抓取到图片地址
$imgurl = 'http://img.infinitynewtab.com/InfinityWallpaper/2_14.jpg'; // 使用默认的图像(默认图像链接可修改为自己的)
}

header("Location: {$imgurl}"); // 跳转至目标图像

好的,你完成了笔记本的所有内容啦!如果你不想打的话,你可以去Github上下载哦!

Github开源链接:https://github.com/Liyuanzhe2008/sql-notebook

快点去添加你的日记吧!~
第一次安装时没有数据表,所以会报错,录入一条日记即可,多多见谅啦!~


一个渣渣,但还是向往着折腾