update 資料表 set 欄位1=值1,欄位2=值2,... [where 篩選條件] [limit 筆數]
<a href="{$action}?op=post_form&sn={$data.sn}" class="btn btn-warning" title="編輯""><i class="fas fa-pencil-alt"></i> 編輯</a>
// 表單
function post_form()
{
global $content, $db, $smarty;
/*****省略***/
// 編輯
if (isset($_GET['sn'])) {
$next_op = 'update';
} else {
// 加入預設值
$content = [
'title' => '',
'directions' => '',
'end' => date("Y-m-d", strtotime("+10 day")),
'priority' => '中',
'assign' => [],
'done' => 0,
];
$next_op = 'add';
}
$smarty->assign('next_op', $next_op);
}
//以流水號取得某筆資料
function find_one($sn = "")
{
global $db;
if (empty($sn)) {
return;
}
$sql = "select * from list where `sn` = '{$sn}'";
if (!$result = $db->query($sql)) {
die(error($db->error));
}
$data = $result->fetch_assoc();
return $data;
}
explode('分割符號',字串變數);
//以流水號取得某筆資料
function find_one($sn = "")
{
global $db;
/**省略**/
$data = $result->fetch_assoc();
// 複選框$data['assign']
$data['assign_arr'] = explode(';', $data['assign']);
return $data;
}
post_form.tpl
<div class="form-group">
<label for="assign">指派對象</label>
<!-- b4-form-check-inline-->
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="assign[]" id="assign_0" value="爸爸" {if "爸爸"|in_array:$content.assign_arr}checked="checked"{/if}>爸爸
</label>
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="assign[]" id="assign_1" value="媽媽" {if "媽媽"|in_array:$content.assign_arr}checked="checked"{/if}>媽媽
</label>
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="assign[]" id="assign_2" value="哥哥" {if "哥哥"|in_array:$content.assign_arr}checked="checked"{/if}>哥哥
</label>
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="assign[]" id="assign_3" value="妹妹" {if "妹妹"|in_array:$content.assign_arr}checked="checked"{/if}>妹妹
</label>
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="assign[]" id="assign_4" value="我" {if "我"|in_array:$content.assign_arr}checked="checked"{/if}>我
</label>
</div>
</div>
sn
編號。
<input type="hidden" name="next_op" value="{$next_op}">
<input type="hidden" name="sn" value="{$content.sn}">
update()
function post_form()
{
global $content, $db, $smarty;
if (isset($_POST['send'])) {
if (isset($_POST['next_op'])) {
/******省略******/
if ($_POST['next_op'] == "update") {
$sn = update();
$_message = empty($_sn) ? "更新失敗" : "更新成功!";
$refresh_url = 'index.php?sn={$sn}';
}
}
die(error($_message, $refresh_url));
}
/******省略******/
}
//function.php更改error()
//跳轉頁
function redirect_page($message, $refresh_url = '', $page_title = '錯誤提示頁')
{
global $smarty;
$smarty->assign('page_title', $page_title);
$smarty->assign('message', $message);
$smarty->assign('refresh_url', $refresh_url);
$smarty->display('templates/error.tpl');
exit();
}
// 表單
function post_form()
{
global $content, $db, $smarty;
if (isset($_POST['send'])) {
if (isset($_POST['next_op'])) {
if ($_POST['next_op'] == "add") {
$sn = add();
if (empty($sn)) {
$_message = "新增失敗";
$page_title = '錯誤提示頁';
$refresh_url = 'index.php';
} else {
$_message = "新增成功!";
$page_title = '成功提示頁';
$refresh_url = 'index.php?sn={$sn}';
}
}
if ($_POST['next_op'] == "update") {
$sn = update();
if (empty($sn)) {
$_message = "更新失敗";
$page_title = '錯誤提示頁';
$refresh_url = 'index.php';
} else {
$_message = "更新成功!";
$page_title = '成功提示頁';
$refresh_url = 'index.php?sn={$sn}';
}
}
}
die(redirect_page($_message, $refresh_url, $page_title));
}
/**************省略*************/
}
//更新清單
function update()
{
global $db;
check_error();
//過濾變數
$sn = (int) $_POST['sn'];
$title = $db->real_escape_string($_POST['title']);
$directions = $db->real_escape_string($_POST['directions']);
$end = $db->real_escape_string($_POST['end']);
$priority = $db->real_escape_string($_POST['priority']);
$assign = $db->real_escape_string(implode(';', $_POST['assign']));
$done = (int) $_POST['done'];
$update_time = date('Y-m-d H:i:s');
// 連線資料庫
$sql = "UPDATE `list` SET
`title`='{$title}',
`directions`='{$directions}',
`end`='{$end}',
`priority`='{$priority}',
`assign`='{$assign}',
`done`='{$done}',
`update_time`='{$update_time}'
WHERE `sn`= '$sn'";
// die($sql);
$db->query($sql) or die(redirect_page($db->error));
return $sn;
}
// 表單
function post_form()
{
global $content, $db, $smarty;
if (isset($_POST['send'])) {
if (isset($_POST['next_op'])) {
if ($_POST['next_op'] == "add") {
$sn = add();
if (empty($sn)) {
$_message = "新增失敗";
$page_title = '錯誤提示頁';
} else {
$_message = "新增成功!";
$page_title = '成功提示頁';
}
$refresh_url = 'index.php';
}
if ($_POST['next_op'] == "update") {
$sn = update();
if (empty($sn)) {
$_message = "更新失敗";
$page_title = '錯誤提示頁';
} else {
$_message = "更新成功!";
$page_title = '成功提示頁';
}
$refresh_url = 'index.php?sn=' . $sn;
}
}
die(redirect_page($_message, $refresh_url, $page_title));
}
//以下省略
}