[PHP-NOTES] note 130334 added to mysql-xdevapi-crudoperationbindable.bind

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

try {
    $conn = new PDO("mysql:host=$servername;dbname=students", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM students");
    $stmt->execute();
    $students = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

if(isset($_POST['create_student']))
{
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $class = $_POST['class'];

    $query = "INSERT INTO students (first_name, last_name, email, phone, class)
    VALUES (:first_name, :last_name, :email, :phone, :class)";
    $query_run = $conn->prepare($query);

    $data = [
        ':first_name' => $first_name,
        ':last_name' => $last_name,
        ':email' => $email,
        ':phone' => $phone,
        ':class' => $class
    ];

    $query_execute = $query_run->execute($data);
    header('Location: ' . $_SERVER['PHP_SELF']);
}

if(isset($_POST['update_student']))
{
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $class = $_POST['class'];
    $student_id = $_POST['student_id']; // aus verstecktem Inputfeld im Modal

    $query = "UPDATE students SET first_name = :first_name, last_name = :last_name, email = :email, phone = :phone, class = :class WHERE id = :id";
    $query_run = $conn->prepare($query);

    $data = [
        ':first_name' => $first_name,
        ':last_name' => $last_name,
        ':email' => $email,
        ':phone' => $phone,
        ':class' => $class,
        ':id' => $student_id
    ];

    $query_execute = $query_run->execute($data);
}

if(isset($_POST['delete_student']))
{
    $student_id = $_POST['delete_student'];
    $query = "DELETE FROM students WHERE id = :id";
    $query_run = $conn->prepare($query);
    $query_execute = $query_run->execute(['id' => $student_id]);
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <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>
    <title>CRUD Practice</title>
</head>
<body>
<div class="container col-md-12 mt-4">
    <h1>Students</h1>
    <table class="table table-striped">
        <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">First</th>
            <th scope="col">Last</th>
            <th scope="col">Email</th>
            <th scope="col">Phone</th>
            <th scope="col">Class</th>
            <th scope="col">Actions</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach($students as $student): ?>
            <tr>
                <td><?= $student['id']; ?></td>
                <td><?= $student['first_name']; ?></td>
                <td><?= $student['last_name']; ?></td>
                <td><?= $student['email']; ?></td>
                <td><?= $student['phone']; ?></td>
                <td><?= $student['class']; ?></td>
                <td>
                    <form action="" method="post" style="display:inline">
                        <button class="btn btn-danger" name="delete_student" value="<?= $student['id']; ?>">Delete</button>
                    </form>
                </td>
                <td>
                    <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#updateModal<?= $student['id']; ?>">
                        Update
                    </button>

                    <div class="modal fade" id="updateModal<?= $student['id']; ?>" tabindex="-1" aria-labelledby="updateModalLabel<?= $student['id']; ?>" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 193.170.70.27)
----
Manual Page -- https://php.net/manual/en/mysql-xdevapi-crudoperationbindable.bind.php
Edit        -- https://main.php.net/note/edit/130334
Del: integrated  -- https://main.php.net/note/delete/130334/integrated
Del: useless     -- https://main.php.net/note/delete/130334/useless
Del: bad code    -- https://main.php.net/note/delete/130334/bad+code
Del: spam        -- https://main.php.net/note/delete/130334/spam
Del: non-english -- https://main.php.net/note/delete/130334/non-english
Del: in docs     -- https://main.php.net/note/delete/130334/in+docs
Del: other reasons-- https://main.php.net/note/delete/130334
Reject      -- https://main.php.net/note/reject/130334
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.