【问题标题】:404 Page not found Codeigniter404 页面未找到
【发布时间】:2013-10-11 06:50:35
【问题描述】:

我是 Codeigniter 的新手。每当我单击“编辑”时,我都会从教程中获取内容。

它链接到..http://localhost/crud/index.php/users/edit/1

我得到了404 Page not Found。

我认为这只是控制器的 uri 或分页问题。 会有什么问题?

users.php - 控制器

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

function __construct()

{

parent::__construct();

#$this->load->helper('url');

$this->load->model('users_model');

}

public function index()

{

$data['user_list'] = $this->users_model->get_all_users();

$this->load->view('show_users', $data);

}
public function add_form()

{

$this->load->view('insert');

}
public function insert_new_user()

{

$udata['name'] = $this->input->post('name');

$udata['email'] = $this->input->post('email');

$udata['address'] = $this->input->post('address');

$udata['mobile'] = $this->input->post('mobile');

$res = $this->users_model->insert_users_to_db($udata);

if($res){

header('location:'.base_url()."index.php/users/".$this->index());

}

}
public function delete_a_user($id)

{

$this->db->where('users.id',$id);

return $this->db->delete('users');

}
public function delete($id)

{

$this->users_model->delete_a_user($id);

$this->index();

}

public function update()

{

$mdata['name']=$_POST['name'];

$mdata['email']=$_POST['email'];

$mdata['address']=$_POST['address'];

$mdata['mobile']=$_POST['mobile'];

$res=$this->users_model->update_info($mdata, $_POST['id']);

if($res){

header('location:'.base_url()."index.php/users/".$this->index());

}

}

}

show_users.php - 视图/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>CI CRUD</title>

<script type="text/javascript">

function show_confirm(act,gotoid)

{

if(act=="edit")

var r=confirm("Do you really want to edit?");

else

var r=confirm("Do you really want to delete?");

if (r==true)

{

window.location="<?php echo base_url();?>index.php/users/"+act+"/"+gotoid;

}

}

</script>

</head>

<body>

<h2> Simple CI CRUD Application </h2>

<table width="600" border="1" cellpadding="5">

<tr>

<th scope="col">Id</th>

<th scope="col">User Name</th>

<th scope="col">Email</th>

<th scope="col">Mobile</th>

<th scope="col">Address</th>

<th scope="col" colspan="2">Action</th>

</tr>

<?php foreach ($user_list as $u_key){ ?>

<tr>

<td><?php echo $u_key->id; ?></td>

<td><?php echo $u_key->name; ?></td>

<td><?php echo $u_key->email; ?></td>

<td><?php echo $u_key->address; ?></td>

<td><?php echo $u_key->mobile; ?></td>

<td width="40" align="left" ><a href="#" onClick="show_confirm('edit',<?php echo $u_key->id;?>)">Edit</a></td>

<td width="40" align="left" ><a href="#" onClick="show_confirm('delete',<?php echo $u_key->id;?>)">Delete </a></td>

</tr>

<?php }?>

<tr>

<td colspan="7" align="right"> <a href="<?php echo base_url();?>index.php/users/add_form">Insert New User</a></td>

</tr>

</table>

</body>

</html>

【问题讨论】:

  • 如果您不熟悉 codeigniter,为什么不试试 grocerycrud.com ?你会更容易安装和使用:)

标签: php codeigniter url pagination crud


【解决方案1】:

您忘记在控制器中添加功能编辑。在您的情况下,它将是这样的:

function edit($id)

{

$mdata['name']=$_POST['name'];
$mdata['email']=$_POST['email'];
$mdata['address']=$_POST['address'];
$mdata['mobile']=$_POST['mobile'];

$res=$this->users_model->update_info($mdata, $id);

...

}

【讨论】:

    【解决方案2】:

    您有几个错误,首先您必须将“更新”方法重命名为“编辑”。

    public function edit($id) {
    
       $mdata['name']    = $_POST['name'];
       $mdata['email']   = $_POST['email'];
       $mdata['address'] = $_POST['address'];
       $mdata['mobile']  = $_POST['mobile'];
    
       $res = $this->users_model->update_info($mdata, $id);
    
       if($res)
          // this is wrong too.. $this->index() execute the index method
          // header('location:'.base_url()."index.php/users/".$this->index());
    
          // use redirect function like this
          redirect('users/index');
    

    }

    PD:我是阿根廷人,我的英语水平很差..

    【讨论】:

      【解决方案3】:

      我在您的控制器中看不到名为“编辑”的功能。 CI URL 默认的工作方式是,第一段“用户”是控制器,类名(用户)。 - 文件和类必须具有相同的名称。 URL“edit”的第二部分将是类中函数的名称。在您的情况下,您的 URL 正在寻找“用户”类和该类中名为“编辑”的函数,而“编辑”不存在!

      添加这个。

      public function edit(){
          // do your stuff here
      }
      

      【讨论】:

        【解决方案4】:

        在您的 users.php - 控制器 中可能是一个类似的函数编辑:

          function edit() {
        
             $this->user_model->edi_user();
             redirect('http://url.com/user/');
        
        
        }
        

        在模型中:

        public function edit_user(){
        
        $id         = $this->session->userdata('user_id');
        $data       = array(
        'name'      => $this->input->post('name'), 
        'lastname'  => $this->input->post('lastname'), 
        );
        
        $this->db->where('id', $id);
        $this->db->update('users', $data);         
        
           }
        

        【讨论】:

          猜你喜欢
          • 2015-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-15
          • 2015-05-19
          • 2016-06-22
          相关资源
          最近更新 更多