1.工具:navicat+phpstorm+phpstudy
2.思路:
3.建库: CREATE DATABASE 库名
4.建表:
CREATE TABLE message
(
id int AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
title VARCHAR(50) NOT NULL,
content VARCHAR(200) NOT NULL,
addtime VARCHAR(50) NOT NULL,
PRIMARY KEY(id)
)
charset = utf8;
5.数据库连接文件:connect.php
<?php
$host = 'localhost';
$username = 'root';
$password = '****';//数据库密码
$dbname = 'message_board';
$port = '3306';
$link = mysqli_connect($host, $username, $password, $dbname, $port);
if (!$link)
{
echo 'connection failed';
}
mysqli_set_charset($link, 'utf8');//设置编码
mysqli_select_db($link, 'message_board');//选择数据库
date_default_timezone_set('Asia/Shanghai');//时区
6.主界面:index.php
<h1 align="center">留言板</h1>
<?php
include_once 'connect.php';
include_once 'add.php';
?>
<table border="1px" width="400px" align="center" >
<?php
$sql = "SELECT * FROM message ORDER BY addtime DESC";//倒序
$query = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($query))
{
?>
<tr>
<td><?=$row['id']?>楼</td>
</tr>
<tr>
<td>用户名 :<?=$row['username']?></td>
</tr>
<tr>
<td>留言标题:<?=$row['title']?></td>
</tr>
<tr>
<td>留言内容:<?=$row['content']?></td>
</tr>
<tr>
<td>留言时间:<?=$row['addtime']?></td>
</tr>
<tr>
<td>
<a href="./edit.php?id=<?=$row['id']?>">修改</a>
<a href="./delete.php?id=<?=$row['id']?>">删除</a>
</td>
</tr>
<?php
}
?>
</table>
7.增:add.php
<?php
include_once 'connect.php';
if ($_POST['submit'])
{
$sql = "insert into message (username, title, content, addtime)
values ('$_POST[username]', '$_POST[title]','$_POST[content]',now())";
$query = mysqli_query($link, $sql);
if(!$query)
{
echo '留言失败<br/>';
echo mysqli_error($link);
}
}
?>
<form action="index.php" method="post" align="center">
用户名 :<input type="text" name="username"/><br/>
留言标题:<input type="text" name="title"/><br/>
<table width="268px" border="0px" align="center">
<tr><td>留言内容:</td></tr>
<tr><td><textarea name="content" rows="5" cols="33"></textarea></td></tr>
</table>
<table width="268px" border="0px" align="center">
<tr>
<td>
<input type="submit" value="发布留言" name="submit"/>
</td>
</tr>
</table>
</form>
8.删:delete.php
<?php
include_once 'connect.php';
include_once 'index.php';
$id = $_GET['id'];
$sql = "DELETE FROM message WHERE id=$id";//写数据库语句时用双引号
$query = mysqli_query($link, $sql);
//echo $query;//查看是否传值成功
if (!$query)
{
echo '删除失败';
echo mysqli_error($link);
}
9.改:edit.php+update.php
edit.php
<?php
include_once 'connect.php';
$id = $_GET['id'];
//echo $id.'<br/>';//查看id是否传值成功
$sql = "SELECT * FROM message WHERE id=$id";//写数据库语句时用双引号
//echo $sql.'<br/>';//查看查询是否成功
$query = mysqli_query($link, $sql);
$row = mysqli_fetch_assoc($query);
?>
<form action="./update.php" method="post" align="center">
<input hidden type="text" name="<?=$row['id']?>"/>
<table width="268px" border="0px" align="center">
<tr><td>修改内容:</td></tr>
<tr><td><textarea name="content" rows="5" cols="33"><?=$row['content']?></textarea></td></tr>
</table>
<table width="268px" border="0px" align="center">
<tr>
<td>
<input type="submit" value="更改留言" name="submit"/>
</td>
</tr>
</table>
</form>
update.php
<?php
include_once 'connect.php';
include_once 'index.php';
$content = $_GET['content'];
$id = $_GET['id'];
$sql = "UPDATE message SET content=$content WHERE id=$id";
$query = mysqli_query($link, $sql);
10.查:index.php (查询结果展示在主界面中)
就是这部分语句
11.效果展示:
主界面
修改界面
总结:写的第一个留言板,出了很多bug,好在github和csdn足够强大,有问题欢迎指出!