medoo::update PHP Метод

update() публичный Метод

public update ( $table, $data, $where = null )
    public function update($table, $data, $where = null)
    {
        $fields = array();
        foreach ($data as $key => $value) {
            preg_match('/([\\w]+)(\\[(\\+|\\-|\\*|\\/)\\])?/i', $key, $match);
            if (isset($match[3])) {
                if (is_numeric($value)) {
                    $fields[] = $this->column_quote($match[1]) . ' = ' . $this->column_quote($match[1]) . ' ' . $match[3] . ' ' . $value;
                }
            } else {
                $column = $this->column_quote($key);
                switch (gettype($value)) {
                    case 'NULL':
                        $fields[] = $column . ' = NULL';
                        break;
                    case 'array':
                        preg_match("/\\(JSON\\)\\s*([\\w]+)/i", $key, $column_match);
                        $fields[] = $column . ' = ' . $this->quote(isset($column_match[0]) ? json_encode($value) : serialize($value));
                        break;
                    case 'boolean':
                        $fields[] = $column . ' = ' . ($value ? '1' : '0');
                        break;
                    case 'integer':
                    case 'double':
                    case 'string':
                        $fields[] = $column . ' = ' . $this->fn_quote($key, $value);
                        break;
                }
            }
        }
        return $this->exec('UPDATE "' . $table . '" SET ' . implode(', ', $fields) . $this->where_clause($where));
    }

Usage Example

Пример #1
2
<?php

/*
*		功能:更新个人信息
*
*/
session_start();
header("Content-type:text/html; charset=utf8");
include_once './medoo/medoo.php';
$database = new medoo();
$Sname = $_SESSION['name'];
if ($_POST['submit']) {
    $name = $_POST['name'];
    $sex = $_POST['sex'];
    $posit = $_POST['posit'];
    $birth = $_POST['birth'];
    $tel = $_POST['tel'];
    $qq = $_POST['qq'];
    $sql = $database->update("user", array("name" => $name, "sex" => $sex, "depart" => $posit, "birth" => $birth, "tel" => $tel, "qq" => $qq), array("name" => $Sname));
    // 判断是否更新成功
    if ($sql) {
        echo "<script> alert('更新信息成功!'); history.go(-1);</script>";
    } else {
        echo "<script> alert('更新信息失败!'); history.go(-1);</script>";
    }
}
All Usage Examples Of medoo::update