summaryrefslogtreecommitdiffstats
path: root/cardEdit.php
blob: 9fce311940db0852f6753b2af19a8f3c89921a61 (plain) (blame)
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
<?php
session_start();

require_once "dbCredentials.php";
require_once "consts.php";
checkLoggedIn();

$card = $_SESSION["edited_card"];
$adding = $card["id"] === null;
if ($card["id"] === null)
    $id = -1;
else
    $id = $card["id"];

if (isset($_POST["body"]) && isset($_POST["title"])) {
    $pdo = new PDO("mysql:dbname=$dbName;host=$dbHost;charset=utf8mb4", $dbUser, $dbPassword);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    if ($id === -1)
        $sql = "INSERT INTO `cms_card`(`title`, `body`) VALUES (:title, :body)";
    else
        $sql = "UPDATE `cms_card` SET `title`= :title, `body`=:body WHERE `id`=:id";

    $stmt = $pdo->prepare($sql);

    if ($id === -1) {
        $stmt->execute([
            "title" => $_POST["title"],
            "body" => nl2br($_POST["body"]),
        ]);
    } else {
        $stmt->execute([
            "title" => $_POST["title"],
            "body" => nl2br($_POST["body"]),
            "id" => $_POST["id"],
        ]);
    }

    $_POST["edited_card"] = null;
    header("Location: cardsList.php");
    exit();
}
?>
<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">

    <title>Edit card</title>
</head>

<body class="bg-dark text-white">
    <div style="margin: 0 auto; width: 50%;">
        <!--  class="container"> -->
        <div style="margin: 5px auto; width: 50%;">
            <h4>Card <?= 14 ?></h4>
            <hr style="margin-left: -150%; margin-top: 0; width: 100vw;">
            <form action="cardEdit.php" method="post" style="width: 140%">
                <input type="hidden" name="id" value="<?= $id ?>">
                <label for="title">Title:</label><br>
                <input type="text" name="title" id="title" value="<?= $card["title"] ?>" required><br>
                <label for="body">Body:</label><br>
                <textarea id="body" name="body" rows="4" cols="50" required><?= $card["body"] ?></textarea><br>
                <?php
                if ($adding === true)
                    echo "<button type='submit' class='btn btn-warning' style='float: right;'>Dodaj</button>";
                else
                    echo "<button type='submit' class='btn btn-info' style='float: right;'>Zapisz</button>";
                ?>
            </form><br><br>
            <h3 style="float: left; color: red;"><?= $error ?></h3>
        </div>
    </div>
    <!-- Optional JavaScript; choose one of the two! -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
</body>

</html>