You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

153 lines
4.1 KiB

<?php
$id = "";
$accounttype = "";
$accountid = "";
$pwd = "";
$name = "";
$tel = "";
$address = "";
$email = "";
$lineid = "";
$wechatid = "";
$phone_call_help = "";
$chat_for_help = "";
$remote_help = "";
$repairerid = "";
$manager = "";
$creater = "";
$create_at = "";
$id_error = "";
$accounttype_error = "";
$accountid_error = "";
$pwd_error = "";
$name_error = "";
$tel_error = "";
$address_error = "";
$email_error = "";
$lineid_error = "";
$wechatid_error = "";
$phone_call_help_error = "";
$chat_for_help_error = "";
$remote_help_error = "";
$repairerid_error = "";
$manager_error = "";
$creater_error = "";
$create_at_error = "";
$department_id_error = "";
$role_id_error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
include "account-user-input.php";
// 檢查連接是否成功
if ($link->connect_errno) {
echo "連接 MySQL 失敗:" . $link->connect_error;
exit();
}
// 開始事務
$link->begin_transaction();
try {
if (in_array($accounttype, ["B", "E", "M", "W"])) {
if (empty($department_id)) {
$department_id_error = "部門未填寫";
throw new Exception('部門未填寫');
}
if (empty($role_id)) {
$role_id_error = "職別未填寫";
throw new Exception('職別未填寫');
}
}
// 新增帳號 table account
$sql = "INSERT INTO account (
accounttype, accountid, pwd, name, tel,
address, email, lineid, wechatid, phone_call_help,
chat_for_help, remote_help, repairerid, manager, department_id,
role_id, creater, create_at
)VALUES(
?,?,?,?,?,?,?,?,?,?,
?,?,?,?,?,?,?,?
)
";
$stmt = $link->prepare($sql);
$stmt->bind_param(
'ssssssssssssssssss',
$i_data['accounttype'],
$i_data['accountid'],
$i_data['pwd'],
$i_data['name'],
$i_data['tel'],
$i_data['address'],
$i_data['email'],
$i_data['lineid'],
$i_data['wechatid'],
$i_data['phone_call_help'],
$i_data['chat_for_help'],
$i_data['remote_help'],
$i_data['repairerid'],
$i_data['manager'],
$i_data['department_id'],
$i_data['role_id'],
$i_data['creater'],
$i_data['create_at']
);
$stmt->execute();
// 新增權限 table account_auth
if (in_array($accounttype, ["B", "E", "M", "W"])) {
$sql = "INSERT INTO account_auth (accountid, permission)
SELECT '$accountid', permission FROM department WHERE department_id = ? AND role_id = ?
";
$stmt2 = $link->prepare($sql);
$stmt2->bind_param('ss', $department_id, $role_id);
$stmt2->execute();
}
// 新增帳號 table employee
$sql = "
INSERT INTO employee (
employee_no,
depart_no,
name,
mail
)VALUES(
?,
?,
?,
?
);
";
$stmt3 = $link->prepare($sql);
$stmt3->bind_param('ssss', $i_data['accountid'], $i_data['department_id'], $i_data['name'], $i_data['email']);
$stmt3->execute();
$link->commit();
$url = "location.href='account-index.php?function_name=account&" . $token_link . "';";
redircetTo($url);
} catch (Exception $e) {
// 如果發生任何錯誤,回滾事務
$link->rollback();
// echo "rollback:" . $e->getMessage();
}
// 關閉連接
$link->close();
}
function redircetTo($url)
{
echo "<script type='text/JavaScript'>";
echo "alert('新增失敗');";
echo "location.href='" . $url . "';";
echo "</script>";
}