-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_transfer.php
More file actions
107 lines (91 loc) · 3.65 KB
/
Copy pathprocess_transfer.php
File metadata and controls
107 lines (91 loc) · 3.65 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
session_start();
require_once __DIR__ . '/db_config.php';
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_FLOAT);
$from_account = trim((string)($_POST['from_account'] ?? ''));
$to_account = trim((string)($_POST['to_account'] ?? ''));
if ($amount === false || $amount <= 0 || $from_account === '' || $to_account === '') {
$_SESSION['transfer_error'] = 'Virheellinen syöte.';
header('Location: result.php');
exit;
}
if ($from_account === $to_account) {
$_SESSION['transfer_error'] = 'Lähde- ja kohdetili eivät voi olla samat.';
header('Location: result.php');
exit;
}
$_SESSION['amount'] = $amount;
$_SESSION['from_account'] = $from_account;
$_SESSION['to_account'] = $to_account;
$conn = get_db_connection();
pg_query($conn, 'BEGIN') or die('Ei onnistuttu aloittamaan tapahtumaa: ' . pg_last_error($conn));
$update_from_sql = '
UPDATE TILIT
SET summa = summa - $1
WHERE tilinumero = $2
AND summa >= $1
';
$res_from = pg_query_params($conn, $update_from_sql, [$amount, $from_account]);
if (!$res_from) {
pg_query($conn, 'ROLLBACK') or die('Ei onnistuttu peruuttamaan tapahtumaa: ' . pg_last_error($conn));
$_SESSION['transfer_error'] = 'Virhe lähdetilin päivittämisessä: ' . pg_last_error($conn);
header('Location: result.php');
exit;
}
if (pg_affected_rows($res_from) !== 1) {
pg_query($conn, 'ROLLBACK') or die('Ei onnistuttu peruuttamaan tapahtumaa: ' . pg_last_error($conn));
$_SESSION['transfer_error'] = 'Lähdetilin tilinumero on väärä tai saldoa ei ole tarpeeksi.';
header('Location: result.php');
exit;
}
$update_to_sql = '
UPDATE TILIT
SET summa = summa + $1
WHERE tilinumero = $2
';
$res_to = pg_query_params($conn, $update_to_sql, [$amount, $to_account]);
if (!$res_to) {
pg_query($conn, 'ROLLBACK') or die('Ei onnistuttu peruuttamaan tapahtumaa: ' . pg_last_error($conn));
$_SESSION['transfer_error'] = 'Virhe kohdetilin päivittämisessä: ' . pg_last_error($conn);
header('Location: result.php');
exit;
}
if (pg_affected_rows($res_to) !== 1) {
pg_query($conn, 'ROLLBACK') or die('Ei onnistuttu peruuttamaan tapahtumaa: ' . pg_last_error($conn));
$_SESSION['transfer_error'] = 'Kohdetilin tilinumero on väärä.';
header('Location: result.php');
exit;
}
$select_sql = '
SELECT tilinumero, omistaja
FROM TILIT
WHERE tilinumero = $1 OR tilinumero = $2
';
$res_select = pg_query_params($conn, $select_sql, [$from_account, $to_account]);
if (!$res_select || pg_num_rows($res_select) < 2) {
pg_query($conn, 'ROLLBACK') or die('Ei onnistuttu peruuttamaan tapahtumaa: ' . pg_last_error($conn));
$_SESSION['transfer_error'] = 'Tilien omistajatietojen hakeminen epäonnistui.';
header('Location: result.php');
exit;
}
$from_owner = null;
$to_owner = null;
while ($row = pg_fetch_assoc($res_select)) {
if ($row['tilinumero'] === $from_account) {
$from_owner = $row['omistaja'];
} elseif ($row['tilinumero'] === $to_account) {
$to_owner = $row['omistaja'];
}
}
if ($from_owner === null || $to_owner === null) {
pg_query($conn, 'ROLLBACK') or die('Ei onnistuttu peruuttamaan tapahtumaa: ' . pg_last_error($conn));
$_SESSION['transfer_error'] = 'Tilien omistajatietojen käsittely epäonnistui.';
header('Location: result.php');
exit;
}
pg_query($conn, 'COMMIT') or die('Ei onnistuttu hyväksymään tapahtumaa: ' . pg_last_error($conn));
$_SESSION['from_owner'] = $from_owner;
$_SESSION['to_owner'] = $to_owner;
$_SESSION['transfer_success'] = true;
header('Location: result.php');
exit;