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

使用带有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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!