[PHP-NOTES] note 130343 added to reserved.variables.post

[email protected] ("Anonymous")
Newsgroups php.notes
Message-ID <[email protected]>
<?php
$servername = "localhost";
$username = "root";
$password = "";

try {
    $conn = new PDO("mysql:host=$servername;dbname=crud_project", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

if(isset($_POST['add'])){
    $newTitle = $_POST['title'];
    $newContent = $_POST['content'];
    $stmt = $conn->prepare("INSERT INTO entries (user_id, title, content) VALUES (1, '$newTitle', '$newContent')");
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

if(isset($_POST['delete'])){
    $delete_id = $_POST['delete'];
    $stmt = $conn->prepare("DELETE FROM entries WHERE id = ?");
    $stmt->execute([$delete_id]);
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

if(isset($_POST['update']) && isset($_POST['title']) && isset($_POST['content'])){
    $stmt = $conn->prepare("UPDATE entries SET title = ?, content = ? WHERE id = ?");
    $stmt->execute([$_POST['title'], $_POST['content'], $_GET['edit']]);
}

?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Test Uebung</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container">
    <table class="table table-striped">
        <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">user_id</th>
            <th scope="col">title</th>
            <th scope="col">content</th>
            <th scope="col">created_at</th>
            <th scope="col">options</th>
        </tr>
        </thead>
        <tbody>

        <?php
        $stmt = $conn->prepare("SELECT * FROM entries");
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        foreach ($result as $row) {
            echo "<tr>";
            foreach ($row as $value) {
                echo "<td>" . $value . "</td>";
            }
            echo "<form method='get'>";
            echo "<td><button class='btn btn-primary' name='edit' value='" . $row['id'] . "'> Details </button></td>";
            echo "</form>";
            echo "<form method='post'>";
            echo "<td><button class='btn btn-danger' name='delete' value='" . $row['id'] . "'> Delete </button></td>";
            echo "</form>";
            echo "</tr>";
        }
        ?>

        </tbody>
    </table>
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
        Add
    </button>

    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <form method="post">
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="exampleInputEmail1" class="form-label">Title</label>
                            <input type="text" class="form-control" aria-describedby="emailHelp" id="title" name="title">
                            <div id="emailHelp" class="form-text">Your title here...</div>
                        </div>
                        <div class="mb-3">
                            <label for="exampleInputPassword1" class="form-label">Content</label>
                            <input type="text" class="form-control" id="content" name="content">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="bu
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 193.170.70.27)
----
Manual Page -- https://php.net/manual/en/reserved.variables.post.php
Edit        -- https://main.php.net/note/edit/130343
Del: integrated  -- https://main.php.net/note/delete/130343/integrated
Del: useless     -- https://main.php.net/note/delete/130343/useless
Del: bad code    -- https://main.php.net/note/delete/130343/bad+code
Del: spam        -- https://main.php.net/note/delete/130343/spam
Del: non-english -- https://main.php.net/note/delete/130343/non-english
Del: in docs     -- https://main.php.net/note/delete/130343/in+docs
Del: other reasons-- https://main.php.net/note/delete/130343
Reject      -- https://main.php.net/note/reject/130343
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.