Follow Teacher Zhu’s video "ThinkPHP5 Rapid Development of Enterprise Sites" to write code. When running the admin_edit page, an "undefined array index: is_update" error appears. I don’t know how to solve it. I would like to ask you for help. Thank you!
Admin.php controller code:
//Perform update operation
public function update(Request $request)
{
if ($request->isAjax(true)) {
//Get the submitted data and automatically filter out null values
$data = array_filter($request->param() );
//Set update conditions
$where = ['is_update' => $data['is_update']];
//Update user table
$result = AdminModel::update($data, $where);
//Prompt message for successful update
$status = 1;
$message = 'Update successful';
//If the update fails
if (is_null($result)) {
$status = 0;
$message = 'Update failed';
}
}
return ['status' => $status, 'message' => $message];
}
admin_edit .html code (the html page has been tested and can run normally after deleting the controller logic code):
{layout name="public/layout" /}
<div class="x-body">
<form class="layui-form">
<div class="layui-form-item ">
<label for="username" class="layui-form-label">
<span class="x-red">*</ span>Login name
</label>
<div class="layui-input-inline">
<input type="text" id ="username" name="username" required="" lay-verify="required" autocomplete="off" value="{$admin.username}"
class="layui-input" disabled>
</div>
<div class="layui-form-mid layui-word-aux">
<span class="x- red">*</span>Username cannot be modified
</div>
</div>
<div class="layui- form-item">
<label for="L_email" class="layui-form-label">
<span class="x-red">* </span>Email
</label>
<div class="layui-input-inline">
<input type="text " value="{$admin.email}" id="email" name="email" required="" lay-verify="email" autocomplete="off" class="layui-input">
</div>
<div class="layui-form-mid layui-word-aux">
<span class="x-red"> *</span>
</div>
</div>
<div class="layui-form-item">
<label for="L_pass" class="layui-form-label">
<span class="x-red">*</span>新密码
</label>
<div class="layui-input-inline">
<input type="password" id="password" name="password" required="" lay-verify="password" autocomplete="off" class="layui-input"
value="123456" placeholder="密码">
</div>
<div class="layui-form-mid layui-word-aux">
6到12个字符
</div>
</div>
<!--添加隐藏字段-->
<input type="hidden" name="id" value="{$Think.session.user_info.id}" lay-filter="id">
<input type="hidden" name="is_update" value="{$Think.session.user_info.is_update}" lay-filter="is_update">
<div class="layui-form-item">
<label for="repassword" class="layui-form-label">
</label>
<button class="layui-btn" lay-submit lay-filter="save" id="btnSubmit">
保存
</button>
</div>
</form>
</div>
<script>
layui.use('form', function () {
// $ = layui.jquery;
var form = layui.form;
// ,layer = layui.layer;
//自定义验证规则
form.verify({
password: [/(. ){6,12}$/, '密码必须6到12位']
});
//监听提交
form.on('submit(save)', function () {
$.ajax({
type: 'POST',
url: "{:url('admin/update')}",
data: $(".layui-form").serialize(),
dataType: "json",
success: function (data) {
if (data.status == 1) {
layer.msg(JSON.stringify(data.message), {icon:6,time:2000}, function() {
// 获得frame索引
var index = parent.layer.getFrameIndex(window.name);
//关闭当前frame
parent.layer.close(index);
});
} else {
layer.msg(JSON.stringify(data.message), {icon:5,time:2000});
}
}
});
return false;
});
});
</script>
流金岁月2018-06-17 11:30:20
I found the cause of the error. The code for saving the Session when logging in is as follows: //Session::set('user_id', $userName);Session::set('user_info', $data);The above $data is only The username and password information of admin does not include field information such as is_update. I changed it to this: //Session::set('user_id', $userName);Session::set('user_info', $admin); Bug disappears. Note: Change the Base controller as well: define('USER_ID', Session::get('user_info.id'));