PHP实现小程序留言板功能(二)只能修改删除自己发表的留言

PHP实现小程序留言板功能

这里我实现了一个只能修改和删除自己的留言,如下图所示
在这里插入图片描述
这是接上篇文章做的添加了新功能,我也不多废话了,发修改了和添加的代码

logs.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}}">
  <view style="margin-top:15rpx;">用户名:{{item.uname}}</view>
  <view style="">内容:{{item.content}}</view>
    <navigator wx:if="{{uids == item.uid}}" style="float:left;margin-right:30rpx;color:#0000FF" url="/pages/update/update?id={{item.id}}">修改</navigator>
    <view wx:if="{{uids == item.uid}}" bindtap="deletei" style="margin-button:30rpx;color:#0000FF" data-src="{{item.id}}">删除</view>
</view>

logs.js

Page({
  data: {
  },
  /**
   * 生命周期函数--监听页面加载---获取从其他页面传来的值经行接收
   */
  onLoad: function(options) {
    this.setData({
      id:options.id,
      uid: options.uid,
      uname: options.uname
    })
    var that = this
    that.setData({
      uids:that.data.uid
    })
    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'],
         })
        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)
  },
  deletei: function (e) {
    wx.showModal({
      title: '提示',
      content: '是否确定删除',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: 'http://127.0.0.1/update.php',
            method:"get",
            header: { 'content-type': 'application/json'},
            dataType:'json',
            data:{
              'a':2,
              'id': e.currentTarget.dataset.src
            },
            success:function(){
              wx.showToast({
                title: '删除成功',
                icon: 'none',
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})

这里我说一下,上篇文章的查询留言发表留言PHP没有变,多添加了一个修改页面和一个PHP文件,修改页面如下图所示
在这里插入图片描述
然后就是修改页面和PHP,删除放到了发表留言页面但是后台文件是链接到修改页面的

update.wxml

<form bindsubmit="update">
  <view wx:for="{{updatei}}" wx:key="{{updatei}}">
    <view style="float:left">内容:</view>
    <input style="border:1px solid #ccc" type="text" value="{{item}}" name="content"></input>
  </view>
  <button form-type="submit">修改</button>
</form>

update.js

Page({
  data: {
  },
  onLoad: function (options) {
    this.setData({
      id: options.id,
    })
    var that = this
    wx.request({
      url: 'http://127.0.0.1/update.php',
      data: {
        'a': 1,
        'id':that.data.id
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        that.setData({
          updatei: res.data,
        })
        console.log('查询值',res.data)
      },
    })
  },
  update:function(e){
    wx.showToast({
      title: '修改成功',
      icon: 'none',
    })
    wx.request({
      url: 'http://127.0.0.1/update.php',
      method: "GET",
      header: { 'content-type': 'application/json' },
      data:{
        "id":this.data.id,
        "content":e.detail.value.content
      },
      dataType:'json',
      success:function(res){
        wx.navigateBack({
          delta: 1
        })
      }
    })
    console.log('content',e.detail.value.content)
  },
  
})

update.php

<?php
	class update{
		//查询
		public function select(){
			require_once 'config.inc.php';
			$sql = "select * from wt_blog where id = ?";
			try{
				$stmt = $link -> prepare($sql);
				$stmt -> execute([$_GET['id']]);
				$row = $stmt->fetch(PDO::FETCH_ASSOC);
				//要转成json格式给小程序才可以
				echo json_encode([$row['content']]);
			}catch(PDOException $e){
				die($e->getMessage());
			}	
		}
		//修改
		public function edit(){
			require_once 'config.inc.php';
			$sql = "update wt_blog set content = ? where id = ?";
			try{
				$stmt = $link -> prepare($sql);
				$stmt -> execute([$_GET['content'],$_GET['id']]);
			}catch(PDOException $e){
				die($e->getMessage());
			}
		}
		//删除
		public function delete(){
			require_once 'config.inc.php';
			$sql = 'delete from wt_blog where id=?';
			try{
				$stmt = $link -> prepare($sql);
				$stmt -> execute([$_GET['id']]);
			}catch(PDOException $e){
				die($e->getMessage());
			}
		}
	}
	$a = new update();
	if($_GET['a'] == 1){
		$a->select();
	}elseif($_GET['a'] == 2){
		$a->delete();
	}else{
		$a->edit();
	}
?>

页面没做美化的,有什么问题欢迎评论留言,我会及时回复你的。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值