線上書籍

Home

[1062] PHP7入門

<?php require_once 'smarty/libs/Smarty.class.php'; $smarty = new Smarty; $db = link_db(); $op = isset($_POST['op']) ? filter_var($_POST['op']) : ''; switch ($op) { case 'insert': $sn = insert_article(); header("location: index.php?sn={$sn}"); break; default: break; } // $smarty->assign('now', date("Y年m月d日 H:i:s")); $smarty->display('admin.tpl'); //儲存文章 function insert_article() { global $db; $title = $db->real_escape_string($_POST['title']); $content = $db->real_escape_string($_POST['content']); $sql = "INSERT INTO `article` (`title`, `content`, `create_time`, `update_time`) VALUES ('{$title}', '{$content}', NOW(), NOW())"; $db->query($sql) or die($db->error); $sn = $db->insert_id; return $sn; } //連線到資料庫 function link_db() { $db = new mysqli('localhost', 'root', '12345', 'reporter'); if ($db->connect_error) { die('無法連上資料庫:' . $db->connect_error); } $db->set_charset("utf8"); return $db; }