数据库结构
![数据库结构](https://i-blog.csdnimg.cn/blog_migrate/70daa89d93a67cfc901786d5e5aa245d.png)
小程序展示页面
![小程序主页展示](https://i-blog.csdnimg.cn/blog_migrate/ffda00c3eb92cb094b2b9690b894699f.png)
小程序wxml页
<text>pages/day6/day6.wxml</text>
<block wx:if="{{arr1.length > 0}}">
<view class="view1" wx:for="{{arr1}}" wx:key="index">
<view class="view2">楼:{{item.id}} </view>
<view hidden class="view2">QQ:{{item[3]}} </view>
<image src="http://q1.qlogo.cn/g?b=qq&nk={{item.qq}}&s=640" mode="widthFix" />
<view class="view2">姓 名:{{item.name}} </view>
<view class="view2">内 容:{{item.text}} </view>
<view class="view2">留言时间:{{item.time}} </view>
<view class="view2">更新时间:{{item.updateT}} </view>
<view class="view3">
<view bindtap="clickMe" data-id="{{item.id}}" > 编辑 </view>
<view >删 除 </view>
</view>
</view>
</block>
小程序wxss页
.view1 {
margin-top: 10rpx;
margin-bottom: 40rpx;
border-bottom: 2rpx solid gray;
}
.view1 view{
margin-left: 20rpx;
margin-top: 20rpx;
margin-bottom: 10rpx;
}
.view3 {
display: flex;
padding-left: 60%;
}
image{
padding-top: 10rpx;
margin-left: 30rpx;
width: 150rpx;
}
小程序js页
Page({ clickMe(e) {
console.log(e.target.dataset.id);
let nid = e.target.dataset.id
wx.navigateTo({
url: '/pages/edit/edit?id=' + nid,
})
},
data: {
arr1: [],
},
newBtnDown(my_url) {
wx.request({
url: my_url,
data: {
userName: JSON.stringify('Administrator'),
passWord: JSON.stringify('123456'),
},
method: 'post',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: res => {
console.log(res);
if (res.statusCode == 200) {
this.setData({
arr1: res.data
})
} else {
console.log("加载失败");
wx.showToast({
title: '加失败!',
icon: 'error',
duration: 2000
})
}
},
fail: res => {
console.log('出错了');
}
})
},
onLoad(options) {
this.newBtnDown('http://www.456.com/mboard/miniapp/api.php')
},
})
php接口文件
<?php
header('Content-Type:text/html;charset=utf-8');
require_once 'database.php';
$sql = "SELECT * FROM `message` ORDER BY `message` . `id` DESC ";
$result = $conn->query($sql);
$arr = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$id = $row["id"];
$name = $row["name"];
$text = $row["text"];
$qq = $row["qq"];
$time = $row["time"];
$updateT = $row["updateT"];
$temp_arr = [
"id" => $id,
"name" => $name,
"text" => $text,
"qq" => $qq,
"time" => $time,
"updateT" => $updateT,
];
array_push($arr, $temp_arr);
}
} else {
echo "0结果";
}
header("Content-type:application/json");
echo json_encode($arr);
$conn->close();
php 数据库连接文件 database.php
<?php
$servername = "localhost:3306";
$username = "root";
$password = "baota123456";
$dbname = "mboard";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}