我尝试在codeigniter中创建一个函数,以便用户能够更改其用户信息。
当我填写表格并提交时,记录不会更新,并显示空白页面。 (我确实启用了PHP错误) 总共有4种不同的表格在同一个视图页面上。 这就是我在视图文件中的内容:
<div class="main">
<table class="gegevensupdaten">
<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>
<form action="<?php echo base_url() . "GwController/update_adres"; ?>" method="post">
<tr>
<td><h4>Adres wijzigen</h4></td>
<td><?php echo form_input(array('id'=>'straat', 'name'=>'straat', 'placeholder' => 'Nieuw straatnaam', 'size'=>70));?></td>
<td><?php echo form_input(array('id'=>'huisnummer', 'name'=>'huisnummer', 'placeholder' => 'Nieuwe huisnummer', 'size'=>70));?></td>
<td><?php echo form_input(array('id'=>'woonplaats', 'name'=>'woonplaats', 'placeholder' => 'Nieuw stad/woonplaats', 'size'=>70));?></td>
<td><?php echo form_input(array('id'=>'postcode', 'name'=>'postcode', 'placeholder' => 'Nieuwe postcode', '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" class="btn btn-primary">Opslaan</button></td>
</tr>
</form>
<form action="<?php echo base_url() . "GwController/update_wachtwoord"; ?>" method="post">
<tr>
<td><h4>Wachtwoord wijzigen</h4></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'user_id', 'name'=>'user_id', 'value' => $_SESSION['user_id'], 'size'=>70));?></td>
<td><?php echo form_input(array('type'=>'password','id'=>'nieuwwachtwoord', 'name'=>'nieuwwachtwoord', 'placeholder' => 'Uw nieuwe wachtwoord: ', 'size'=>70));?></td>
<td><?php echo form_input(array('type'=>'password','id'=>'herhaalwachtwoord', 'name'=>'herhaalwachtwoord', 'placeholder' => 'Herhaal uw nieuwe wachtwoord', 'size'=>70));?></td>
<td><?php echo form_input(array('type'=>'password','id'=>'wachtwoord', 'name'=>'wachtwoord', 'placeholder' => 'Uw oude wachtwoord:', 'size'=>70));?></td>
<td><button type="submit" class="btn btn-primary">Opslaan</button></td>
</tr>
</form>
<form action="<?php echo base_url() . "GwController/update_beschrijving"; ?>" method="post">
<tr>
<td> <h4>Beschrijving wijzigen</h4></td>
<td> <?php echo form_textarea(array('type'=>'textarea','id'=>'beschrijving', 'name'=>'beschrijving', 'placeholder' => 'Uw nieuwe beschrijving..','cols'=>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" class="btn btn-primary">Opslaan</button></td>
</tr>
</form>
</table>
</div>
这是我的更新控制器功能:
<?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');
}
}
//Controller functie om het adres van een gebruiker te veranderen
function update_adres()
{
$id= $this->input->post('user_id');
$data = array(
'straat' => $this->input->post('straat'),
'huisnummer' => $this->input->post('huisnummer'),
'woonplaats' => $this->input->post('woonplaats'),
'postcode' => $this->input->post('postcode')
);
$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');
}
}
//Controller functie om gebruikerswachtwoord te veranderen
function update_wachtwoord()
{
$id= $this->input->post('user_id');
$nieuwwachtwoord = $this->input->post('nieuwwachtwoord');
$herhaalwachtwoord = $this->input->post('herhaalwachtwoord');
if($nieuwwachtwoord == $herhaalwachtwoord){
$data = array(
'wachtwoord' => $this->input->post('nieuwwachtwoord'),
);
$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');
}
}
//Controller functie voor profiel beschrijving bewerken
function update_beschrijving()
{
$id= $this->input->post('product_id');
$data = array(
'beschrijving' => $this->input->post('beschrijving'),
);
$this->update_model->update_wachtwoord($id,$data);
header ('location:https://kadokado-ferran10.c9users.io/user/profile');
}
}
}
这些都是我的模特功能:
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');
}
//Model update gebruiker adres veranderen
function update_adres($id,$data){
$this->db->where('user_id', $id);
$this->db->update('users', $data);
$_SESSION['straat'] = $data['straat'];
$_SESSION['huisnummer'] = $data['huisnummer'];
$_SESSION['postcode'] = $data['postcode'];
$_SESSION['woonplaats'] = $data['woonplaats'];
header ('location:https://kadokado-ferran10.c9users.io/user/profile');
}
//Model update gebruiker wachtwoord veranderen
function update_wachtwoord($id,$data){
$this->db->where('user_id', $id);
$this->db->update('users', $data);
$_SESSION['wachtwoord'] = $data['wachtwoord'];
header ('location:https://kadokado-ferran10.c9users.io/user/profile');
}
//Model update gebruiker beschrijving veranderen
function update_beschrijving($id,$data){
$this->db->where('user_id', $id);
$this->db->update('users', $data);
$_SESSION['beschrijving'] = $data['beschrijving'];
header ('location:https://kadokado-ferran10.c9users.io/user/profile');
}
}
有人知道它为什么不起作用吗? 任何形式的帮助表示赞赏
答案 0 :(得分:0)
我怀疑你实际启用了PHP错误,或者你的问题会很明显,因为你会收到通知。
以function update_email()
为例。
你这样做:
if ($user["user_id"]) {
$this->Update_model->update_email($id, $data);
}
但是$user
不是一个数组(如果您启用了这些数据,则会显示通知),因为您正在使用row()
,例如$user = $query->row();
您需要像对象一样访问用户:
if ($user->user_id) {
$this->Update_model->update_email($id, $data);
}
现在:&#34;记录未获得更新,并显示空白页面&#34; ......由于之前提到的错误,您从未点击$this->Update_model->update_email
,因此无法更新。如果您预计会出现错误,那么您应该始终在控制器中重定向该错误。 不要重新定位您的模型,就像您现在正在做的那样,否则您将获得一个空白页面,因为如果模型永远不会被击中,则不会发生任何事情。
此外,我必须问你为什么这样做,在你做这个的//check gebruiker in database
的每个功能中都可以轻松地做到这一点:
<?php
class Curr_user_model extends CI_Model {
/**
* Gets current user id
* @return int|null
*/
public function id() {
return isset($_SESSION['user_id']) ? $_SESSION['user_id'] : NULL;
}
/**
* Checks if user exists for realz
* @return boolean TRUE if user with id() exists in db, FALSE otherwise
*/
public function exists() {
$id = $this->id();
if (is_null($id)) {
return false;
}
$this->db->where('user_id', $id);
return $this->db->count_all_results('users') == 1;
}
/**
* Gets current user details as object
* @return object
*/
public function details() {
$query = $this->db->get_where('users', array('user_id' => $this->id()));
return $query->row();
}
}
// extend this on any page that should be fully protected, and the
// curr_user field will be globally accessible to the extended classes!
class MY_Admin_Controller extends CI_Controller {
public $curr_user;
public function __construct() {
parent::__construct();
$this->load->model('curr_user_model');
if ($this->curr_user->exists()) {
$this->curr_user = $this->curr_user_model->details();
} else {
// redirect to login, user isn't supposed to be here... yet
redirect('somepage/login');
}
}
}
// user will *never* be able to reach anything in this controller unless he is logged in
class GwController extends MY_Admin_Controller {
public function index() {
print_r($this->curr_user);
echo 'user id: ' . $this->curr_user->user_id;
}
}
只需使用MY_Admin_Controller
扩展任何控制器,如果用户未登录,始终将重定向到登录页面。防止您不断检查用户是否被允许访问页面或执行和操作。请记住,MY_Admin_Controller.php
位于core
文件夹中。