Php 插入带有CodeIgniter活动记录的数据
我的问题是关于使用CodeIgniter活动记录插入数据。指南中有一个使用数组插入数据的示例:Php 插入带有CodeIgniter活动记录的数据,php,mysql,codeigniter,insert,Php,Mysql,Codeigniter,Insert,我的问题是关于使用CodeIgniter活动记录插入数据。指南中有一个使用数组插入数据的示例: $data = array( 'title' => 'My title' , 'name' => 'My Name' , 'date' => 'My date' ); $this->db->insert('mytable', $data); 我想知道是否有其他方法可以插入活动记录数据,例如使用类似的语法: $this -> db -&
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
我想知道是否有其他方法可以插入活动记录数据,例如使用类似的语法:
$this -> db -> select (*);
$this -> db -> from ('users');
$this -> db -> where('id', $id);
$this -> db -> limit(1);
$query = $this->db->get();
谢谢。您可以使用set()
方法
根据CI文档,您可以使用以下语法:
$this->db->set('name', $name);
$this->db->insert('mytable');
这将生成以下查询:
INSERT INTO mytable (name) VALUES ('{$name}')
希望这就是您要寻找的。您可以使用set()
方法
**its very simple just follow the following steps**<br>
**first prepare a dabase in mysql**<br>
-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Jul 02, 2013 at 03:48 AM
-- Server version: 5.5.27
-- PHP Version: 5.4.7
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `persons`
--
-- --------------------------------------------------------
--
-- Table structure for table `person`
CREATE TABLE IF NOT EXISTS `person` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`person_name` varchar(50) NOT NULL,
`person_address` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1728 ;
--
-- Dumping data for table `person`
--
INSERT INTO `person` (`id`, `person_name`, `person_address`) VALUES
(1726, 'soma', 'goma'),
(1727, 'roma', 'toma');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<br>
**Open Codeigniter and make the follwoing changes in the following files**<br>
**make changes in autoload.php**<br>
**it is located at**<br>
**C:\xampp\htdocs\CI_person\application\config**<br>
from
**$autoload['libraries'] = array('');** to
**$autoload['libraries'] = array('database');**$config['base_url']<br>
make changes in config.php
**it is located at**<br>
**C:\xampp\htdocs\CI_person\application\config**<br>
$config['base_url'] = ''; to
$config['base_url'] = 'http://localhost/';
make changes in database.php
**it is located at**<br>
**C:\xampp\htdocs\CI_person\application\config**<br>
$db['default']['hostname'] = '';<br>
$db['default']['username'] = '';<br>
$db['default']['password'] = '';<br>
$db['default']['database'] = '';<br>
$db['default']['dbdriver'] = 'mysql';<br>
to<br>
$db['default']['hostname'] = 'localhost';<br>
$db['default']['username'] = 'root';<br>
$db['default']['password'] = '';<br>
$db['default']['database'] = 'persons';<br>
$db['default']['dbdriver'] = 'mysql';<br>
now open the views folder located at<br>
**C:\xampp\htdocs\CI_person\application**<br>
**Create a view persons.php in C:\xampp\htdocs\CI_person\application\views**
<html>
<head>
</head>
<body>
<script language="javascript">
function clicked()
{
alert("You clicked");
document.getElementById('check').value='I';
}
</script>
<form id="entry" name="entry" method="post" >
<input type="hidden" name="check" id="check" value="">
Person Name
<input type="text" name="person_name" id="person_name" />
<br />
Person Address
<input type="text" name="person_address" id="person_address" />
<input type="submit" name="button" id="button" value="Submit" onClick="clicked()">
<table cellpadding="2px" width="600px" border="2">
<?php
foreach ($persons as $person){
$id = $person['id'];
$name = $person['person_name'];
$address = $person['person_address'];
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $address; ?><br /> </td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>
**create controller named persons.php in Controller folder**<br>
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Persons extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Person_model');
}
public function index()
{
$person_name = $this->input->post('person_name');
$person_address = $this->input->post('person_address');
$check= $this->input->post('check');
if ($check=="")
{
$this->data['persons'] = $this->Person_model->get_all();
$this->load->view('persons', $this->data);
}
if ($check=="I")
{
/*$this->load->model('Person_model');
$this->Person_model->insert_to_db($this->input->post('person_name'),$this->input->post('person_address'));
*/
$this->Person_model->person_name = $person_name;
$this->Person_model->person_address =$person_address;
$this->Person_model->insert_into_db();
$this->data['persons'] = $this->Person_model->get_all();
$this->load->view('persons', $this->data);
}
}
}
**now create model as defined below in Models folder**<br>
<?php
class Person_model extends CI_Model
{
public $person_name;
public $person_address;
public function __construct()
{
}
public function get_all()
{
$this->db->select('id,person_name,person_address');
$query= $this->db->get('person');
return $query->result_array();
}
function insert_into_db()
{
$f1 = $this->person_name;
$f2 = $this->person_address;
$this->db->query("INSERT INTO person(person_name,person_address) VALUES('$f1','$f2')");
}
}
?>
**Save all and run**
根据CI文档,您可以使用以下语法:
$this->db->set('name', $name);
$this->db->insert('mytable');
这将生成以下查询:
INSERT INTO mytable (name) VALUES ('{$name}')
希望这就是你想要的。**非常简单,只需按照以下步骤操作**
**its very simple just follow the following steps**<br>
**first prepare a dabase in mysql**<br>
-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Jul 02, 2013 at 03:48 AM
-- Server version: 5.5.27
-- PHP Version: 5.4.7
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `persons`
--
-- --------------------------------------------------------
--
-- Table structure for table `person`
CREATE TABLE IF NOT EXISTS `person` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`person_name` varchar(50) NOT NULL,
`person_address` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1728 ;
--
-- Dumping data for table `person`
--
INSERT INTO `person` (`id`, `person_name`, `person_address`) VALUES
(1726, 'soma', 'goma'),
(1727, 'roma', 'toma');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<br>
**Open Codeigniter and make the follwoing changes in the following files**<br>
**make changes in autoload.php**<br>
**it is located at**<br>
**C:\xampp\htdocs\CI_person\application\config**<br>
from
**$autoload['libraries'] = array('');** to
**$autoload['libraries'] = array('database');**$config['base_url']<br>
make changes in config.php
**it is located at**<br>
**C:\xampp\htdocs\CI_person\application\config**<br>
$config['base_url'] = ''; to
$config['base_url'] = 'http://localhost/';
make changes in database.php
**it is located at**<br>
**C:\xampp\htdocs\CI_person\application\config**<br>
$db['default']['hostname'] = '';<br>
$db['default']['username'] = '';<br>
$db['default']['password'] = '';<br>
$db['default']['database'] = '';<br>
$db['default']['dbdriver'] = 'mysql';<br>
to<br>
$db['default']['hostname'] = 'localhost';<br>
$db['default']['username'] = 'root';<br>
$db['default']['password'] = '';<br>
$db['default']['database'] = 'persons';<br>
$db['default']['dbdriver'] = 'mysql';<br>
now open the views folder located at<br>
**C:\xampp\htdocs\CI_person\application**<br>
**Create a view persons.php in C:\xampp\htdocs\CI_person\application\views**
<html>
<head>
</head>
<body>
<script language="javascript">
function clicked()
{
alert("You clicked");
document.getElementById('check').value='I';
}
</script>
<form id="entry" name="entry" method="post" >
<input type="hidden" name="check" id="check" value="">
Person Name
<input type="text" name="person_name" id="person_name" />
<br />
Person Address
<input type="text" name="person_address" id="person_address" />
<input type="submit" name="button" id="button" value="Submit" onClick="clicked()">
<table cellpadding="2px" width="600px" border="2">
<?php
foreach ($persons as $person){
$id = $person['id'];
$name = $person['person_name'];
$address = $person['person_address'];
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $address; ?><br /> </td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>
**create controller named persons.php in Controller folder**<br>
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Persons extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Person_model');
}
public function index()
{
$person_name = $this->input->post('person_name');
$person_address = $this->input->post('person_address');
$check= $this->input->post('check');
if ($check=="")
{
$this->data['persons'] = $this->Person_model->get_all();
$this->load->view('persons', $this->data);
}
if ($check=="I")
{
/*$this->load->model('Person_model');
$this->Person_model->insert_to_db($this->input->post('person_name'),$this->input->post('person_address'));
*/
$this->Person_model->person_name = $person_name;
$this->Person_model->person_address =$person_address;
$this->Person_model->insert_into_db();
$this->data['persons'] = $this->Person_model->get_all();
$this->load->view('persons', $this->data);
}
}
}
**now create model as defined below in Models folder**<br>
<?php
class Person_model extends CI_Model
{
public $person_name;
public $person_address;
public function __construct()
{
}
public function get_all()
{
$this->db->select('id,person_name,person_address');
$query= $this->db->get('person');
return $query->result_array();
}
function insert_into_db()
{
$f1 = $this->person_name;
$f2 = $this->person_address;
$this->db->query("INSERT INTO person(person_name,person_address) VALUES('$f1','$f2')");
}
}
?>
**Save all and run**
**首先在mysql中准备一个数据库**
--phpMyAdmin SQL转储
--版本3.5.2.2
-- http://www.phpmyadmin.net
--
--主持人:127.0.0.1
--生成时间:2013年7月2日凌晨3:48
--服务器版本:5.5.27
--PHP版本:5.4.7
设置SQL\u MODE=“零上无自动值”;
设置时区=“+00:00”;
/*!40101 SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;
/*!40101 SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;
/*!40101设置@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;
/*!40101集合名称utf8*/;
--
--数据库:`人`
--
-- --------------------------------------------------------
--
--表`人的表结构`
如果不存在“person”,则创建表(
`id`int(9)非空自动增量,
`人名'varchar(50)不为空,
`人员地址'varchar(50)不为空,
主键(`id`)
)ENGINE=InnoDB默认字符集=latin1自动增量=1728;
--
--正在转储“person”表的数据`
--
在“person”(`id`、`person\u name`、`person\u address`)值中插入
(1726,“索马”、“戈马”),
(1727年,“罗马”、“托马”);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT*/;
/*!40101集字符集结果=@旧字符集结果*/;
/*!40101设置排序规则\连接=@OLD\ u排序规则\连接*/;
**打开Codeigniter并在以下文件中进行以下更改**
**在autoload.php中进行更改**
**它位于**
**C:\xampp\htdocs\CI\u person\application\config**
从…起
**$autoload['libraries']=数组('');**到
**$autoload['libraries']=array('database')**$配置['base_url']
在config.php中进行更改
**它位于**
**C:\xampp\htdocs\CI\u person\application\config**
$config['base_url']='';到
$config['base_url']='http://localhost/';
在database.php中进行更改
**它位于**
**C:\xampp\htdocs\CI\u person\application\config**
$db['default']['hostname']=''
$db['default']['username']=''
$db['default']['password']=''
$db['default']['database']=''
$db['default']['dbdriver']='mysql'
到
$db['default']['hostname']='localhost'
$db['default']['username']='root'
$db['default']['password']=''
$db['default']['database']='persons'
$db['default']['dbdriver']='mysql'
现在打开位于
**C:\xampp\htdocs\CI\u person\application**
**在C:\xampp\htdocs\CI\u person\application\views中创建一个view persons.php**
函数单击()
{
警告(“您点击了”);
document.getElementById('check')。value='I';
}
人名
个人地址
**在控制器文件夹**
**非常简单,只需按照以下步骤操作**
**首先在mysql中准备一个数据库**
--phpMyAdmin SQL转储
--版本3.5.2.2
-- http://www.phpmyadmin.net
--
--主持人:127.0.0.1
--生成时间:2013年7月2日凌晨3:48
--服务器版本:5.5.27
--PHP版本:5.4.7
设置SQL\u MODE=“零上无自动值”;
设置时区=“+00:00”;
/*!40101 SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;
/*!40101 SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;
/*!40101设置@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;
/*!40101集合名称utf8*/;
--
--数据库:`人`
--
-- --------------------------------------------------------
--
--表`人的表结构`
如果不存在“person”,则创建表(
`id`int(9)非空自动增量,
`人名'varchar(50)不为空,
`人员地址'varchar(50)不为空,
主键(`id`)
)ENGINE=InnoDB默认字符集=latin1自动增量=1728;
--
--正在转储“person”表的数据`
--
在“person”(`id`、`person\u name`、`person\u address`)值中插入
(1726,“索马”、“戈马”),
(1727年,“罗马”、“托马”);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT*/;
/*!40101集字符集结果=@旧字符集结果*/;
/*!40101设置排序规则\连接=@OLD\ u排序规则\连接*/;
**打开Codeigniter并在以下文件中进行以下更改**
**在autoload.php中进行更改**
**它位于**
**C:\xampp\htdocs\CI\u person\application\config**
从…起
**$autoload['libraries']=数组('');**到
**$autoload['libraries']=array('database')**$配置['base_url']
在config.php中进行更改
**它位于**
**C:\xampp\htdocs\CI\u person\application\config**
$config['base_url']='';到
$config['base_url']='http://localhost/';
在database.php中进行更改
**它位于**
**C:\xampp\htdocs\CI\u person\application\config**
$db['default']['hostname']=''
$db['default']['username']=''
$db['default']['password']=''
$db['default']['database']=''
$db['default']['dbdriver']='mysql'
到
$db['default']['hostname']='localhost'
$db['default']['username']='root'
$db['default']['password']=''
$db['default']['database']='persons'
$db['default']['dbdriver']='mysql'
现在打开位于
**C:\xampp\htdocs\CI\u person\application**
**在C:\xampp\htdocs\CI\u person\application\views中创建一个view persons.php**
函数单击()
{
警告(“您点击了”);
docum