PHP实现小程序留言板功能(一)
接着我前面写的功能这里我们开始写一个留言板,写小程序和写网页完全是两个概念可把我整惨了,我这里先写了查询留言和发表留言,后面我还要做一系列的功能,首先是数据库结构把
create table wt_blog(
id int PRIMARY KEY AUTO_INCREMENT,
uid int not null,
uname varchar(22) not null,
content varchar(60) not null
)
我是通过登陆成功传值然后经行留言,首先看看登陆JS的代码把,需要其他的代码可以去我前面几篇文章里看
index.JS
Page({
data: {
},
login: function (e) {
wx.request({
url: 'http://127.0.0.1/login.php',//不要用localhost必须用127.0.0.1来访问--本地不支持HTTPS
method: "get",
header: {
'content-type': 'application/json'
},
data: {
"username": e.detail.value.username,
"password": e.detail.value.password
},
success(res) {
if (res.data['0'].uid != undefined && res.data['0'].uname == e.detail.value.username) {
wx.redirectTo({
url: '/pages/logs/logs?uid=' + res.data["0"].uid +'&uname='+res.data['0'].uname,//把uid和uname传到留言板页面
})
} else {
wx.showToast({
title: '登陆失败',
icon: 'none'
})
}
}
})
console.log("账号", e.detail.value.username)
console.log("密码", e.detail.value.password)
}
})
然后就是我们的留言板的页面了我这里没有进行页面美化,如下图所示
废话就不说了直接上代码,需要注意的都注释好了
.wxml
<form bindsubmit="liuyanban">
<view style="float:left;margin-left:15rpx">留言内容:</view><input type="text" name="content"style="border:1px solid #ccc"></input>
<button form-type="submit">提交留言</button>
</form>
<view wx:for="{{liuyantext}}" wx:key="{{liuyantext}}">//获取js查询出来的值赋名为liuyantext
<view style="margin-top:15rpx;">用户名:{{item.uname}}</view>
<view style="border-bottom:1px solid #ccc;">内容:{{item.content}}</view>
</view>
.js
Page({
data: {
},
/**
* 生命周期函数--监听页面加载---获取从其他页面传来的值经行接收
*/
onLoad: function(options) {
this.setData({
uid: options.uid,
uname: options.uname
})
var that = this
wx.request({
url: 'http://127.0.0.1/liuyanban.php',
data:{
'a':1//本人不太熟悉小程序怎么调用后台的方法就直接传个值进行判断
},
header: { 'content-type': 'application/json'},
method: 'GET',
dataType: 'json',
success: function(res) {
that.setData({
liuyantext: res.data['0'],//把查询出来的值赋名为liuyantext
})
console.log('查询值', res.data['0'])
},
})
},
liuyanban: function(e) {
if(e.detail.value.content != ""){
var that = this
wx.request({
url: 'http://127.0.0.1/liuyanban.php',
data: {
"uid":this.data.uid,
"uname":this.data.uname,
"content":e.detail.value.content
},
header: { 'content-type': 'application/x-www-form-urlencoded'},
method: 'POST',
dataType: 'json',
success: function(res) {
console.log('插入数据值:',res)
},
})
}
console.log('留言内容',e.detail.value.content)
console.log('uid:', this.data.uid)
console.log('uname:', this.data.uname)
}
})
config.inc.php
<?php
header('content-type:text/html;charset=utf-8');
session_start();
try{
$link = new PDO('mysql:host=localhost;port=3306;dbname=wt_kp5b','root','root');//数据库名什么的记得改成你自己的
$link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die('数据库连接出错:'.$e->getMessage());
}
?>
.php
<?php
class blog{
public function select(){
require_once 'config.inc.php';
$sql = 'select * from wt_blog order by id desc';
try{
$stmt = $link -> prepare($sql);
$stmt -> execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
//要转成json格式给小程序才可以
$results[] = $row;
}
echo json_encode([$results]);
}catch(PDOException $e){
die($e->getMessage());
}
}
public function content(){
require_once 'config.inc.php';
$sqli = 'insert into wt_blog(uid,uname,content) values(?,?,?)';
try{
$stmt = $link->prepare($sqli);
$stmt->execute([$_POST['uid'],$_POST['uname'],$_POST['content']]);
echo "提交留言成功";
}catch(PDOException $e){
die($e->getMessage());
}
}
}
$a = new blog();
if($_GET['a'] == 1){
$a->select();
}else{
$a->content();
}
?>
有什么问题欢迎评论留言,我会及时回复你的。