-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-edit-api.php
62 lines (51 loc) · 1.39 KB
/
data-edit-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
require __DIR__. '/parts/__connect_db.php';
require __DIR__. '/parts/__admin_required.php';
header('Content-Type: application/json');
$output = [
'success' => false,
'postData' => $_POST,
'code' => 0,
'error' => ''
];
// TODO: 檢查資料格式
// email_pattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
// mobile_pattern = /^09\d{2}-?\d{3}-?\d{3}$/;
if(empty($_POST['sid'])){
$output['code'] = 405;
$output['error'] = '沒有 sid';
echo json_encode($output, JSON_UNESCAPED_UNICODE);
exit;
}
if(mb_strlen($_POST['name'])<2){
$output['code'] = 410;
$output['error'] = '姓名長度要大於 2';
echo json_encode($output, JSON_UNESCAPED_UNICODE);
exit;
}
if(! preg_match('/^09\d{2}-?\d{3}-?\d{3}$/', $_POST['mobile'])){
$output['code'] = 420;
$output['error'] = '手機號碼格式錯誤';
echo json_encode($output, JSON_UNESCAPED_UNICODE);
exit;
}
$sql = "UPDATE `address_book` SET
`name`=?,
`email`=?,
`mobile`=?,
`birthday`=?,
`address`=?
WHERE `sid`=?";
$stmt = $pdo->prepare($sql);
$stmt->execute([
$_POST['name'],
$_POST['email'],
$_POST['mobile'],
$_POST['birthday'],
$_POST['address'],
$_POST['sid'],
]);
if($stmt->rowCount()){
$output['success'] = true;
}
echo json_encode($output, JSON_UNESCAPED_UNICODE);