TP5初见访问方法无效_thinkphp_1
TP5 把自己的前端页面渲染_thinkphp5_2
TP5查询分页功能_thinkphp5_3
TP5 增加用户功能 thinkphp5_4
TP5 编辑修改用户 thinkphp5_5
问题描述: 如标题
问题解决:
解决思路: 把用户的 ID 通过 get 传入,执行 SQL 即可
后端代码: 没有加验证,自己加。
public function deleteUser() // delete 方法
{
if (request() -> isGet()) {
$data = request() -> param();
$res = Db::table('bought_user') -> where('id', $data['user_id']) -> delete();
if($res > 0) {
return 'success';
} else {
return 'failed';
}
}
}
前端代码:
// 删除功能( 把隐藏的 id 传过去 )
let btnDelete = document.getElementsByClassName('btn-delete');
let btnDeleteLen = btnDelete.length;
for (let i = 0; i < btnDeleteLen; i++) {
btnDelete[i].addEventListener('click', function () {
let checkBool = confirm("Do you want to delete this user?");
if (!checkBool) {
return null;
}
let userId = this.parentElement.parentElement.firstElementChild.innerText;
_ajax({
url: '/index/Index/deleteUser',
type: 'get',
data: {
user_id: userId
},
success: function (data) {
msgBoxShow("已经删除该用户: " + data, 'rgb(28, 184, 65)');
setTimeout(function () {
window.location.reload();
}, 1000);
},
error: function (data) {
msgBoxShow("删除失败:" + data, 'red');
}
})
})
}
前端所有代码:
<!Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="购药查询系统">
<title>购药查询系统</title>
<link rel="stylesheet" href="/static/css/pure.css">
<link rel="stylesheet" href="/static/css/more_css.css">
</head>
<body>
<div class="custom-wrapper pure-g" id="menu">
<div class="pure-u-1-3">
<div class="pure-menu pure-menu-horizontal">
<ul class="pure-menu-list">
<li class="pure-menu-item pure-menu-selected"><a href="/index/Index/queryUser" class="pure-button pure-button-primary">首页</a></li>
<li class="pure-menu-item pure-menu-selected"><button onclick="addUserFun(this);" class="pure-button button-warning add-user-btn">添加</button></li>
<li class="pure-menu-item pure-menu-selected"><button type="button" class="button-warning pure-button add-user-cancel-btn" onclick="hideAllTable(this);">取消添加</button></li>
<li class="pure-menu-item pure-menu-has-children pure-menu-allow-hover">
<select name="" id="" class = "select_box" onchange="selectBoxOnchange(this.options[this.options.selectedIndex].value);">
<option value="" disabled selected>=每页显示=</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="5"> 5</option>
<option value="10"> 10</option>
</select>
</li>
</ul>
</div>
</div>
<div class="pure-u-1-3">
<h1 style="text-align: center;">购药查询系统 | {$buyTitle}</h1>
</div>
<div class="pure-u-1-3">
<form class="pure-form pure-form-aligned" method="POST" action="">
<fieldset>
<label for="name">购药人姓名</label>
<input id="name" type="text" placeholder="购药人姓名">
<button type="submit" class="pure-button pure-button-primary">查询</button>
</fieldset>
</form>
</div>
</div>
<div class="pure-form pure-form-add">
<button type="button" class="button-secondary pure-button show_another_table" onclick="showAnotherTable();">表单形式填写</button>
<fieldset class="pure-group textarea-form">
<textarea class="pure-input-1-2 textareaForm" rows=4 placeholder="马冬梅 366202200102284222 女 34 18050666666 拉娃省 满满市 呱呱县 神奇大厦B橦1818 否 否 否 是 无 否 是 否 否 马冬梅 18050666666 小儿止咳糖浆 2021-09-06"></textarea>
<div class='inputForm'>
<input type="text" class="pure-input-1-4" name