-
Notifications
You must be signed in to change notification settings - Fork 0
/
bet.php
83 lines (63 loc) · 2.06 KB
/
bet.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
require_once 'init.php';
$errors = [];
$required = ['cost', 'id'];
$numbers = ['cost'];
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !(empty($user))) {
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$sql = "SELECT *
FROM lots
WHERE lots.id=$id";
$result = mysqli_query($link, $sql);
if (!$result){
$error = mysqli_error($link);
show_error($error);
}
if (!mysqli_num_rows($result)) {
http_response_code(404);
show_error('Лот с этим идентификатором не найден');
}
$lot = mysqli_fetch_array($result, MYSQLI_ASSOC);
$user_id = $user['id'];
if($lot['user_id'] == $user_id){
http_response_code(400);
show_error('это ваш лот');
}
$sql = "SELECT *
FROM bets
WHERE lots_id=$id AND user_id=$user_id";
$result = mysqli_query($link, $sql);
if (!$result){
$error = mysqli_error($link);
show_error($error);
}
if (mysqli_num_rows($result)) {
$errors['user'] = 'ставка уже сделана';
}
foreach ($required as $key) {
if (empty(trim($_POST[$key]))) {
$errors[$key] = 'Это поле надо заполнить';
}
}
foreach ($numbers as $key) {
if (empty($errors[$key])) {
$errors = array_merge($errors, validate_number($_POST[$key], $key));
}
}
if ($lot['price']+$lot['amount_step']>intval($_POST['cost'])){
$errors['cost'] = 'слишком мало';
}
if (!count($errors)) {
$sql = "INSERT INTO bets (amount, user_id, lot_id) VALUES(?, ?, ?)";
$stmt = db_get_prepare_stmt($link, $sql, [
$_POST['cost'], $user['id'], $_POST['id']
]);
$res = mysqli_stmt_execute($stmt);
if ($res) {
header('Location: lot.php?id='.isset($_POST['id']) ? intval($_POST['id']) : 0);
}
}
} else {
header('Location: index.php');
}
?>