我想使用CodeIgniter,PHP和MySQL创建一个简单的应用程序,它使用表单将数据收集到MySQL数据库中。以下是我的代码 - 对于form1.php,我正在使用 -
<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>
对于控制器 -
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');
用户模型看起来像 -
<?php
class User_model extends CI_model{
public function add_user($user){
$this->db->insert('user', $user);
}
}
?>
现在,当我将http://127.0.0.1/samplesurvey/放入浏览器时,我得到了我的表单。我正在使用WAMP
服务器,我的应用程序的名称是samplesurvey。
但是当我提交时,我收到错误:
在此服务器上找不到请求的URL / samplesurvey / form / add_user。
我已经像其他答案一样编辑了.htaccess
文件和Apache模块,但它无法正常工作。
在定义表单的操作URL时,最好使用site_url
而不是base_url
。
<form role="form" method="post" action="<?php echo site_url('form/add_user'); ?>">
错误的.htaccess
可能是问题,所以暂时删除.htaccess
文件。
确保在config.php
$config['base_url'] = 'http://127.0.0.1/samplesurvey';
$config['index_page'] = 'index.php';
它应该完美。