Home  >  Article  >  Backend Development  >  PHP CI framework inserts one or more sql records example, cisql_PHP tutorial

PHP CI framework inserts one or more sql records example, cisql_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:59860browse

php CI framework inserting one or more sql records example, cisql

1. Insert a record

$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);

$this->db->insert('mytable', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')

2. Insert multiple records

$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'My title1' ,
'name' => 'My Name1' ,
'date' => 'My date1'
)
);

$this->db->insert_batch('mytable', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'),('My title1', 'My name1', 'My date1')

How to use one SQL statement to insert multiple records?

Use insert and select combined statements

Examples are as follows:
Insert data into the Sales table:
INSERT INTO Sales(EmployeeID,ProductID,SupplierID,CustomerID,
OrderDate,UnitPrice,Total,Quantity,Discount )
SELECT e.EmployeeID, p.ProductID, s.SupplierID,
c.CustomerID, o.OrderDate, od.UnitPrice,
od.Quantity*od.UnitPrice*(1.0-od.Discount) Total,
Od.Quantity, od.Discount
from Orders o,[Order Details] od, Employees e,
Products p, Suppliers s, Customers c
where (o.OrderID = od. OrderID) and
(o.EmployeeID = e.EmployeeID) and
(o.CustomerID = c.CustomerID) and
(od.ProductId = p.ProductID) and
(p.SupplierID = s.SupplierID);

PHP+mysql How to insert multiple records in the same field in a message? For example, there are multiple picture addresses

You will understand after looking at a simple example

insert into news (type_id,userid) values ​​('5','8'),('6','10'),('11' ,'55');

If id is the primary key, you don’t need to write
when inserting

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/852376.htmlTechArticlephp CI framework insert one or more sql records example, cisql 1. Insert a record $data = array(' title' = 'My title' ,'name' = 'My Name' ,'date' = 'My date');$this-db-insert('mytable...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn