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.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)