一、CI版本
开发版本3.1.2 下载地址:https://github.com/bcit-ci/CodeIgniter/archive/3.1.2.zip
二、开发步骤
1、解压文件到www/ci 目录下
2、创建数据库 myci 后建表 user
CREATE TABLE `user` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`uname` varchar(20) DEFAULT NULL,
`age` int(2) DEFAULT NULL,
`product` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=gbk;
3、修改数据库配置文件 ci\application\config\database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'myci',
'dbdriver' => 'mysqli',
4、修改routes.php 下的初始路径
$route['default_controller'] = 'index1';
5、model文件下创建 M_user.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_user extends CI_Model {
public function insert(){
$data=array(
'uname'=>$this->input->post('username'),
'age'=>$this->input->post('age'),
'product'=>$this->input->post('product')
);
$this->db->insert('user', $data);
return $this->db->insert_id();
}
}
6、controllers 下有两个文件 Index1.php 和 Add.php
Index1.php内容如下
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Index1 extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$data = array(
'title'=>'我的标题',
'message'=>'这里是我的内容'
);
$this->load->view('index',$data);
}
}
Add.php内容如下
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Add extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('M_user');
//$this->load->database();
}
public function insert(){
if ($this->M_user->insert() > 0)
{
echo "<script>alert('增加成功');window.history.back(-1);</script>";
}
else
{
echo "no";
}
}
}
7、view 文件夹下创建文件 index.php
<html>
<body>
<div id="container">
<?php
echo $message;
?>
<form name="myform" action="index.php/Add/insert" method="post">
姓名:<input type="text" name="username" width="200px"><p>
年龄:<input type="text" name="age" width="200px"><p>
备注:<input type="text" name="product" width="200px"><p>
<input type="submit" value="提交">
</form>
</body>
</html>
到此,全部完成,可将数据写入数据库,一个简单的增功能就完成了。