public function insert($table, array $data, $and = '')
    {
        if (isset($this->prepared[$table])) {
            return $this->execute($table, $data);
        }
        $single = (count(array_filter(array_keys($data), 'is_string')) > 0) ? $data : false;
        if ($single) {
            $data = array_keys($data);
        }
        if (stripos($table, ' INTO ') !== false) { // eg. 'OR IGNORE INTO table'
            $query = "INSERT {$table} ";
        } else {
            $query = "INSERT INTO {$table} ";
        }
        $query .= '('.implode(', ', $data).') VALUES ('.implode(', ', array_fill(0, count($data), '?')).') '.$and;
        $stmt = $this->prepare($query);
        if ($single && $stmt) {
            $id = $this->insert($stmt, array_values($single));
            $this->close($stmt);

            return $id;
        }

        return $stmt;
    }
public function insert($table, array $data, $and = '')
    {
        if (isset($this->prepared[$table])) {
            return $this->execute($table, $data);
        }
        $single = (count(array_filter(array_keys($data), 'is_string')) > 0) ? $data : false;
        if ($single) {
            $data = array_keys($data);
        }
        if (stripos($table, ' INTO ') !== false) { // eg. 'OR IGNORE INTO table'
            $query = "INSERT {$table} ";
        } else {
            $query = "INSERT INTO {$table} ";
        }
        $query .= '('.implode(', ', $data).') VALUES ('.implode(', ', array_fill(0, count($data), '?')).') '.$and;
        $stmt = $this->prepare($query);
        if ($single && $stmt) {
            $id = $this->insert($stmt, array_values($single));
            $this->close($stmt);

            return $id;
        }

        return $stmt;
    }
public function insert ( $ table , array $ data , $ and = '' ) { if ( isset ( $ this -> prepared [ $ table ] ) ) { return $ this -> execute ( $ table , $ data ) ; } $ single = ( count ( array_filter ( array_keys ( $ data ) , 'is_string' ) ) > 0 ) ? $ data : false ; if ( $ single ) { $ data = array_keys ( $ data ) ; } if ( stripos ( $ table , ' INTO ' ) !== false ) { // eg. 'OR IGNORE INTO table' $ query = "INSERT {$table} " ; } else { $ query = "INSERT INTO {$table} " ; } $ query .= '(' . implode ( ', ' , $ data ) . ') VALUES (' . implode ( ', ' , array_fill ( 0 , count ( $ data ) , '?' ) ) . ') ' . $ and ; $ stmt = $ this -> prepare ( $ query ) ; if ( $ single && $ stmt ) { $ id = $ this -> insert ( $ stmt , array_values ( $ single ) ) ; $ this -> close ( $ stmt ) ; return $ id ; } return $ stmt ; }
Insert new records into a database table.

@param string|int $table Either the database table name, or the prepared statement id you got from calling this the first time.  You can also include *'INTO'* here which is handy for prepending a qualifier eg. *'OR IGNORE INTO table'*.
@param array      $data  Either the table column names (when preparing a statement), or a row of values (to insert).  If you are only inserting one record and want to save yourself some typing, then make this an ``array(name => value, ...)`` of columns.
@param string     $and   Anything you would like to add at the end of the query eg. *'ON DUPLICATE KEY UPDATE ...'*.

@return bool|int Either ``false`` if there was an error, a prepared ``$stmt`` to keep passing off as the **$table**, or the ``$id`` of the row you just inserted.  Don't forget to ``$db->close($stmt)``.

```php
if ($stmt = $db->insert('employees', array('id', 'name', 'title'))) {
$db->insert($stmt, array(101, 'John Smith', 'CEO'));
$db->insert($stmt, array(102, 'Raj Reddy', 'Sysadmin'));
$db->insert($stmt, array(103, 'Jason Bourne', 'Developer'));
$db->insert($stmt, array(104, 'Jane Smith', 'Sales Manager'));
$db->insert($stmt, array(105, 'Rita Patel', 'DBA'));
$db->close($stmt);
}

if ($db->insert('OR IGNORE INTO employees', array(
'id' => 106,
'name' => "Little Bobby'); DROP TABLE employees;--",
'title' => 'Intern',
))) {
echo $db->log('count'); // 1 - It worked!
}
```
Insert new records into a database table .