我是Codeigniter 3的初学者。我试图在视图中提交两个表单值。这是代码:
<?php
class User extends CI_Controller{
public function __construct(){
parent::__construct();
if(!isset($_SESSION['user_logged'])){
redirect("auth/login","refresh");
}
}
public function profile(){
$this->load->view('profile');
}
public function members(){
if ($this->input->post('promote')!==FALSE){
$this->form_validation->set_rules('username','Userpromote','required');
if($this->form_validation->run() == TRUE){
$data = array(
'subadmin' => $_POST['username']
);
$this->db->insert('sub', $data);
redirect("user/members", "refresh");
}
} elseif ($this->input->post('demote')!==FALSE){
$this->form_validation->set_rules('subdemote','Userdemote','required');
if($this->form_validation->run() == TRUE){
$data2 = $_POST['subdemote'];
$this->db->delete('sub', array('subadmin' => $data2 ));
redirect("user/members", "refresh");
}
}
$this->load->view('members');
}
public function products(){
$this->load->view('products');
}
}
但是问题是,只有“升级”部分有效,而“降级”部分无效。如果我删除“升级”部分,则“降级”存储工作正常。你能告诉我我在做什么错吗?
答案 0 :(得分:0)
在我看来,您有一个逻辑问题。您在else if块上有“ demote”代码,这意味着一旦满足if语句,根本就不会达到else if。同样,您在语句末尾使用了重定向,这意味着您将在那一刻被重定向,并且脚本的其余部分将不会执行。如果这样,您可以尝试两个单独的方法:
public function members(){
if ($this->input->post('promote')!==FALSE){
$this->form_validation->set_rules('username','Userpromote','required');
if($this->form_validation->run() == TRUE){
$data = array(
'subadmin' => $_POST['username']
);
$this->db->insert('sub', $data);
//redirect("user/members", "refresh");
}
}
if ($this->input->post('demote')!==FALSE){
$this->form_validation->set_rules('subdemote','Userdemote','required');
if($this->form_validation->run() == TRUE){
$data2 = $_POST['subdemote'];
$this->db->delete('sub', array('subadmin' => $data2 ));
//redirect("user/members", "refresh");
}
}
$this->load->view('members');
}