404页面未找到您请求的页面未找到。CodeIgniter

3

enter image description here

你好,我对codeigniter比较新,请问能否帮我解决一个问题呢?几天前我的代码还可以正常运行,但是现在打开却显示“页面未找到”错误。主要的功能是将用户添加到数据库中。请您帮助我并提供建议。以下是我的model、controller和view代码:

Model文件

Userinsert_model.php

<?php
class Userinsert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){

$this->db->insert('students', $data);
}

public function datafetch(){
    $fetch= $this->db->get('students'); 
    return $fetch->result();

}

}



?>

控制器文件

Userinsert_controller.php(用户插入控制器)

<?php

class Userinsert_controller extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->model('Userinsert_model');
}
function index() {

$this->load->library('form_validation');

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

$this->form_validation->set_rules('dname', 'Username', 'required|min_length[5]|max_length[15]');

$this->form_validation->set_rules('demail', 'Email', 'required|valid_email');

$this->form_validation->set_rules('dmobile', 'Mobile No.', 'required|regex_match[/^[0-9]{10}$/]');

$this->form_validation->set_rules('daddress', 'Address', 'required|min_length[10]|max_length[50]');

if ($this->form_validation->run() == FALSE) {
$this->load->view('Userinsert_view');
} else {
$data = array(
'Student_Name' => $this->input->post('dname'),
'Student_Email' => $this->input->post('demail'),
'Student_Mobile' => $this->input->post('dmobile'),
'Student_Address' => $this->input->post('daddress')
);
$this->Userinsert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
$this->load->view('Userinsert_view', $data);
}
}
public function fetch() {

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

        $data["all_data"]= $this->Userinsert_model->datafetch();
$this->load->helper('form');
    $this->load->view('Usershow_view', $data);  
}
}



?>

查看文件

Userinsert_view.php

<html>
<head>
<title>Insert Data Into Database Using CodeIgniter Form</title>
</head>
<body>

<div id="container">
<?php echo form_open('Userinsert_controller'); ?>

<h1>Insert Data Into Database Using CodeIgniter</h1><hr/>
<?php if (isset($message)) { ?>
<CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER><br>
<?php } ?>
<?php echo form_label('Student Name :'); ?> <?php echo form_error('dname'); ?><br />
<?php echo form_input(array('id' => 'dname', 'name' => 'dname')); ?><br />

<?php echo form_label('Student Email :'); ?> <?php echo form_error('demail'); ?><br />
<?php echo form_input(array('id' => 'demail', 'name' => 'demail')); ?><br />

<?php echo form_label('Student Mobile No. :'); ?> <?php echo form_error('dmobile'); ?><br />
<?php echo form_input(array('id' => 'dmobile', 'name' => 'dmobile', 'placeholder' => '10 Digit Mobile No.')); ?><br />

<?php echo form_label('Student Address :'); ?> <?php echo form_error('daddress'); ?><br />
<?php echo form_input(array('id' => 'daddress', 'name' => 'daddress')); ?><br />

<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?><br/>
<div id="fugo">

</div>
</div>
</body>
</html>

你能展示一下config/routes.php文件的样子吗?404错误可能是由路由配置不正确引起的。 - Kuru
$route['default_controller'] = 'welcome'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; - dawood basharat
将Userinsert_controller.php重命名为userinsert_controller.php。 - Dilip Patel
@amarnath 谢谢,我会阅读它。 - dawood basharat
@Shihas,我按照你说的做了,但问题仍然存在。 - dawood basharat
显示剩余7条评论
3个回答

3
在您的config.php中替换
$config['index_page'] = 'index.php';  

$config['index_page'] = '';

愉快编码 (Y) - Shihas
我可以向您再请教一些问题吗? - dawood basharat
当然可以。如果你想修复一些错误,你可以提出新的问题。 - Shihas
我有一些新问题,与会话相关!如果您不介意,我可以得到您的电子邮件或其他联系方式吗?如果您觉得不方便,请不要介意。 - dawood basharat

1
class Userinsert_controller extends CI_Controller { 重命名为 class Userinsert extends CI_Controller { ,然后通过 example.com/index.php/userinsert 访问该站点。

先生,问题仍然存在。 - dawood basharat
控制器是否在applications/controller中? - Lars
是的,这就是为什么我把它命名为Userinsert_controller,只是为了让我更容易记住。但是我已经改变了它的名称Userinsert。 - dawood basharat
我该如何在评论中上传屏幕截图 @lars - dawood basharat
@amarnath,我希望我理解了你的意思并且已经发布了你需要的图片。 - dawood basharat
显示剩余3条评论

1
您的application/config/routes.php文件必须如下所示:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
|   example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
|   https://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are three reserved routes:
|
|   $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
|   $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router which controller/method to use if those
| provided in the URL cannot be matched to a valid route.
|
|   $route['translate_uri_dashes'] = FALSE;
|
| This is not exactly a route, but allows you to automatically route
| controller and method names that contain dashes. '-' isn't a valid
| class or method name character, so it requires translation.
| When you set this option to TRUE, it will replace ALL dashes in the
| controller and method URI segments.
|
| Examples: my-controller/index -> my_controller/index
|       my-controller/my-method -> my_controller/my_method
*/
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

/* CUSTOM ROUTES */
$route['userinsert'] = 'Userinsert';

基本上,您需要具有以下路由。
$route['userinsert'] = 'Userinsert';

让我再检查一遍。 - dawood basharat
/* 自定义路由 */ $route['userinsert'] = 'Userinsert'; - dawood basharat
你如何访问该网站?使用哪个URL? - Lars
这是网址 http://localhost/codeIgniter/CodeIgniter-3.1.4/index.php/Userinsert - dawood basharat
它使用了你建议的URL,但问题仍然存在。 - dawood basharat
先生,非常感谢您的帮助,您向我提出了一些新的查找错误的方法。下次我一定会期待它们。再次感谢您,先生。 - dawood basharat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接