我正在尝试创建一个功能,以便用户可以在数据库中更改他们的电子邮件。当我提交表单时,我收到错误,当我尝试提交表单时,登录用户的电子邮件不会更改。
我的控制器:
<?php
class GwController extends CI_Controller {
// index functie - hierin laad ik de view file 'gegevenswijigen' in het mapje views.
public function index()
{
$this->load->view('gegevenswijzigen');
}
function __construct()
{
parent::__construct();
//Laad het Update_model in models folder
$this->load->model('Update_model');
}
//Controller functie om email van een gebruiker te veranderen
function update_email()
{
$id= $this->input->post('user_id');
$data = array(
'email' => $this->input->post('email'));
$wachtwoord = $_POST['wachtwoord'];
//check gebruiker in database
$this->db->select('*');
$this->db->from('users');
$this->db->where(array('user_id'=>$_SESSION['user_id'], 'wachtwoord' => $wachtwoord));
$query = $this->db->get();
$user = $query->row();
//Als gebruiker bestaat
if ($user->user_id) {
$this->Update_model->update_email($id, $data);
header ('location:https://kadokado-ferran10.c9users.io/user/profile');
}
}
}
我的模特功能:
<?php
class Update_model extends CI_Model{
// Model update functie om email te veranderen
function update_email($id,$data){
$this->db->where('user_id', $id);
$this->db->update('users', $data);
$_SESSION['email'] = $data['email'];
//header ('location:https://kadokado-ferran10.c9users.io/user/profile');
}
}
我的观点:
<form action="<?php echo base_url() . "GwController/update_email"?>" method="post">
<tr>
<td><h4>E-mail adres wijzigen</h4></td>
<td><?php echo form_input(array('type'=>'email','id'=>'email', 'name'=>'email', 'placeholder' => 'Nieuw e-mail adres:', 'size'=>70));?></td>
<td><?php echo form_input(array('type'=>'password','id'=>'wachtwoord', 'name'=>'wachtwoord', 'placeholder' => 'Uw wachtwoord:', 'size'=>70));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'user_id', 'name'=>'user_id', 'value' => $_SESSION['user_id'], 'size'=>70));?></td>
<td><button type="submit" name="emailwijzigen" class="btn btn-primary">Opslaan</button></td>
</tr>
</form>
这是我在尝试填写表单并提交时收到的错误:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/GwController.php
Line Number: 45
Backtrace:
File: /home/ubuntu/workspace/application/controllers/GwController.php
Line: 45
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
这是第45行:
if ($user->user_id) {
感谢任何形式的帮助,谢谢
答案 0 :(得分:0)
让我们完成问题排查步骤。
echo 'sessid:'.print_r($_SESSION['user_id']);
。echo 'any results?:' . $query->num_rows();
$user = $query->row();
echo $this->db->get_compiled_select();
而不是$query = $this->db->get();
手动检查,并尝试使用phpmyadmin中生成的查询进行手动。num_rows()
没有返回任何行,那么条件$this->db->where(array('user_id'=>$_SESSION['user_id'], 'wachtwoord' => $wachtwoord));
正在评估数据库中不存在user_id
和密码的用户echo $_POST['wachtwoord'];
另外请考虑这样的错误情况。您的代码应始终考虑到帐户错误。我已经冒昧地注释你的功能,以便妥善处理错误。
function update_email() {
$id = $this->input->post('user_id');
$email = $this->input->post('email');
$wachtwoord = $this->input->post('wachtwoord');
// input->post returns null if not set, we need all these variables
// thus they should all be set
if (is_null($id) || is_null($email) || is_null($wachtwoord)) {
exit('Missing parameters'); // handle this nicer with session flash messages
}
//check gebruiker in database
$this->db->select('*');
$this->db->from('users');
$this->db->where(array('user_id' => $_SESSION['user_id'], 'wachtwoord' => $wachtwoord));
$query = $this->db->get();
// there should only be one valid user for the given
// user_id and password... otherwise we have no results and $user->user_id
// will fail or we have a bigger problem on our hands (multiple users w/
// the same id
if ($query->num_rows() !== 1) {
exit('User error');
}
$user = $query->row();
//Als gebruiker bestaat
// not sure what the point of this conditional statement is...
// could move inside num_rows()
if ($user->user_id) {
$this->Update_model->update_email($id, array('email' => $email));
header('location:https://kadokado-ferran10.c9users.io/user/profile');
} else {
// do something (should always have an else statement in a case like this)
}
}
请进一步阅读:https://codeinphp.github.io/post/controllers-for-frontend-and-backend-in-codeigniter/
这是处理Admin/LoggedIn
和Public
部分的更好方法。