404 Page not found The page you requested was not found. CodeIgniter

enter image description here

Hi I'm brand new to codeigniter. I have a code that worked fine a few days ago, but when I reopened it, it gave me the error of the page you asked for not found. basically code about adding a user to the database. Please help me here and I need your suggestions. Here is my model, controller and view codes.

Model file

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();

}

}



?>

      

Controller file

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);  
}
}



?>

      

View file

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>

      

+3


source to share


3 answers


To config.php

replace

$config['index_page'] = 'index.php';  

      



to

$config['index_page'] = '';

      

+3


source


Rename class Userinsert_controller extends CI_Controller {

to class Userinsert extends CI_Controller {

and then visit the site via example.com/index.php/userinsert



+1


source


Yours application/config/routes.php

should look like this:

<?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';

      

Basically you need to have the following route.

$route['userinsert'] = 'Userinsert';

      

+1


source







All Articles