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 ""; }