medoo::insert PHP Method

insert() public method

public insert ( $table, $datas )
    public function insert($table, $datas)
    {
        $lastId = array();
        if (!isset($datas[0])) {
            $datas = array($datas);
        }
        foreach ($datas as $data) {
            $values = array();
            $columns = array();
            foreach ($data as $key => $value) {
                array_push($columns, $this->column_quote($key));
                switch (gettype($value)) {
                    case 'NULL':
                        $values[] = 'NULL';
                        break;
                    case 'array':
                        preg_match("/\\(JSON\\)\\s*([\\w]+)/i", $key, $column_match);
                        $values[] = isset($column_match[0]) ? $this->quote(json_encode($value)) : $this->quote(serialize($value));
                        break;
                    case 'boolean':
                        $values[] = $value ? '1' : '0';
                        break;
                    case 'integer':
                    case 'double':
                    case 'string':
                        $values[] = $this->fn_quote($key, $value);
                        break;
                }
            }
            $this->exec('INSERT INTO "' . $table . '" (' . implode(', ', $columns) . ') VALUES (' . implode($values, ', ') . ')');
            $lastId[] = $this->pdo->lastInsertId();
        }
        return count($lastId) > 1 ? $lastId : $lastId[0];
    }

Usage Example

コード例 #1
1
<?php

session_start();
// error_reporting(null);
if (!isset($_SESSION['uid'])) {
    header("Location: login.php");
    exit;
}
date_default_timezone_set('Asia/Shanghai');
include "./medoo.min.php";
$database = new medoo();
if (isset($_GET['qid']) && isset($_POST['reply'])) {
    $name = $database->select("user", "name", array("id" => $_SESSION['uid']));
    $database->insert("answer", array("qid" => $_GET['qid'], "name" => $name[0], "content" => $_POST['reply']));
    $database->update("question", array("isanswer" => 1), array("id" => $_GET['qid']));
    header("Location: my.php");
}
All Usage Examples Of medoo::insert