du229908
2018-06-05 12:39
浏览 130
已采纳

使用带有php和mysql的codeigniter时,在此服务器上找不到请求的URL错误

I want to make a simple app using CodeIgniter, PHP, and MySQL which collects data using a form into MySQL database. The following are my codes - for the form1.php, I'm using -

  <body>

    <div class="container">
    <div class="row">
    <div class="col-md-4 col-md-offset-4">
    <div class="login-panel panel panel-success">
    <div class="panel-heading">
        <h3 class="panel-title">Form</h3>
    </div>
    <div class="panel-body">

    <form role="form" method="post" action="<?php echo base_url('/form/add_user'); ?>">
       Name:<br>
       <input class="form-control" placeholder="Name" type="text" name="name"><br>
       Age: <br>
       <input class="form-control" placeholder="Age" type="number" name="age"><br>
       Gender:<br>
       <input type="radio" name="gender" value="male">Male<br>
       <input type="radio" name="gender" value="female">Female<br><br>
       State:<br>
       <select name="state">
         <option value="maharashtra">Maharashtra</option>
         <option value="west bengal">West Bengal</option>
         <option value="kerela">Kerela</option>
         <option value="bihar">Bihar</option>
       </select><br>
       <br>    
       <input class="btn btn-lg btn-success btn-block" type="submit" value="Submit" name="submit" >
    </form>
    </div>
    </div>
    </div>
    </div>
    </div>
  </body>

For the controller -

class Form extends CI_Controller {

public function __construct(){

        parent::__construct();
      $this->load->helper('url');
      $this->load->model('user_model');
}

public function index()
{
$this->load->view("form1.php");
}

public function add_user(){

    $user=array(
    'name'=>$this->input->post('name'),
    'age'=>$this->input->post('age'),
    'gender'=>$this->input->post('gender'),
    'state'=>$this->input->post('state')
      );
      print_r($user);


    $this->user_model->add_user($user);
    redirect ('form');

And the user model looks like -

<?php
class User_model extends CI_model{

public function add_user($user){
  $this->db->insert('user', $user);
}

}
?>

Now I get my form when I put http://127.0.0.1/samplesurvey/ in the browser. I am using WAMP server and the name of my app is samplesurvey.
But when I submit, I get the error:

The requested URL /samplesurvey/form/add_user was not found on this server.

I have edited the .htaccess file and the Apache module as in other answers but it is not working.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanji8887 2018-06-05 13:16
    已采纳

    It is good practice to use site_url instead of base_url while defining action URL of a form.

    <form role="form" method="post" action="<?php echo site_url('form/add_user'); ?>">
    

    wrong .htaccess can be the issue, so temporarily remove the .htaccess file.

    Make sure in config.php

    $config['base_url'] = 'http://127.0.0.1/samplesurvey'; 
    $config['index_page'] = 'index.php';
    

    It should work perfectly.

    打赏 评论

相关推荐 更多相似问题