我正在做一个学校项目。
我的Users.php控制器中有一个validateUser函数:
public function validateUser()
{
$data = [];
UserModel::validateUser($this->request->getPost('id_user')); // validate the user
if (session()->get('success'))
{
return redirect()->to('admin_panel');
}
else
{
echo view('templates/header', $data);
echo view('admin_panel2',$data);
echo view('templates/footer');
}
}}
这是我的UserModel.php 中的validateUser函数
public static function validateUser($id_user)
{
$model = new UserModel();
$data = ['id_role' => '2'];
$model->update($id_user,$data);
session()->setFlashdata('success', 'Successfuly Validated');
}
我的视图
<div class="container">
<div class="row">
<div class="col-12">
<div class="container">
<p></p>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h6 class="panel-title">
<a data-toggle="collapse" href="#collapse1">
<?php
echo "<h3>Users To Validate</h3>";
?>
</a>
<?php
?>
</h6>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">
<h4></h4>
<ol>
<div class="container">
<div class="row">
<div class="col-12 col-sm8- offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white form-wrapper">
<div class="container">
<?php if (session()->get('success')) : ?>
<div class="alert alert-success" role="alert">
<?= session()->get('success') ?>
</div>
<?php endif; ?>
<?php foreach ($usersToValidate as $row)
echo '<li class="nav-item">' . $row->firstname . ' ' . $row->lastname . '<a class="nav-link active" href="validate_user?id_user='.$row->id_user.'">Validate</a></li>';
?>
<ul class="nav">
</ul>
</div>
</div>
</div>
</div>
</ol>
</div>
<div class="panel-footer">
</div>
</div>
</div>
</div>
</div>
<div class="container">
<hr>
<?php if (isset($permissions)) : ?>
<div class="col-12">
<div class="alert alert-danger" role="alert">
</div>
</div>
<?php else : ?>
<?php endif; ?>
<?php if (isset($validation)) : ?>
<div class="col-12">
<div class="alert alert-danger" role="alert">
<?= $validation->listErrors() ?>
</div>
</div>
<?php else : ?>
<?php endif; ?>
</div>
</div>
</div>
问题是,在我的"users"表中,所有"id_role"记录都被更改为"2"。我只想更新数据库中的一条记录。
知道我做错了什么吗?
我发现了这个问题。我替换了$this->请求->getPost('id_user'(和getGet('id_user'(在我的控制器函数中
public function validateUser()
{
$data = [];
UserModel::validateUser($this->request->getGet('id_user')); // validate the user
if (session()->get('success'))
{
return redirect()->to('admin_panel');
}
else
{
echo view('templates/header', $data);
echo view('admin_panel2',$data);
echo view('templates/footer');
}
}