summaryrefslogtreecommitdiff
path: root/paid.php
blob: ec7e89a0809a4adccef01154e63b1aade45fea63 (plain)
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
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Qarz daftarcha</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="container mt-5">
    <h2>Toladi</h2>
    <form method="post" action="">
        <div class="form-group">
            <label for="miqdor">Pul:</label>
            <input type="number" min="0" class="form-control" id="miqdor" placeholder="10000" name="miqdor">
        </div>
        <button type="submit" class="btn btn-primary">Saqlash</button>
    </form>
</div>

<?php

include "get_users.php";

$id = $_GET["id"];
$existing_money = $conn->query("SELECT miqdor FROM qarzdorlar WHERE id = $id");
$existing_money = $existing_money->fetch_assoc()["miqdor"];

if (array_key_exists("miqdor", $_POST) && $existing_money >= $_POST["miqdor"]) {
    $miqdor = (int) $existing_money - $_POST["miqdor"];
    $sql = "UPDATE qarzdorlar SET miqdor = $miqdor WHERE id = $id";
    $result = $conn->query($sql);

    if ($result === TRUE) {
        header("Location: index.php");
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
} elseif (array_key_exists("miqdor", $_POST) && $existing_money <= $_POST["miqdor"]) {
    header("Location: index.php");
}

?>

</body>
</html>