Edit items in database in PHP

Jack Herer 105 Reputation points
2023-04-25T06:17:08.2266667+00:00

<?php

require_once 'db.php';

$sql = 'SELECT * FROM manufacturer';

$stmt = $conn->prepare($sql);
$stmt->execute();
$manufacturers = $stmt->fetchAll();


$sql = 'SELECT * FROM employee';

$stmt = $conn->prepare($sql);
$stmt->execute();
$employees = $stmt->fetchAll();

$sql = 'SELECT * FROM cars';

$stmt = $conn->prepare($sql);
$stmt->execute();
$cars = $stmt->fetchAll();

$car = null;

 if (isset($_POST['edit_car'], $_POST['car_id'])) {

    $sql = 'UPDATE cars SET model = :model, license_plate = :license_plate, id_employee = :employee WHERE id = :id';

    $stmt = $conn->prepare($sql);
    $stmt->execute([
        ':model' => $_POST['model'],
        ':license_plate' => $_POST['license_plate'],
        ':employee' => $_POST['employee'],
        ':id' => $_POST['car_id'],
    ]);

    header('Location: index.php');
}


if (isset($_GET['id'])) {
    $sql = 'SELECT * FROM cars WHERE id = :id';
    $stmt = $conn->prepare($sql);
    $stmt->execute([':id' => $_GET['id']]);
    $car = $stmt->fetch();
}

?>

<!doctype html>

        <html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>

    <title>Driver</title>
</head>
<body style="">
<form action="" method="post" style="margin: 20px 100px 0 100px">
    <div style="display: flex; justify-content: space-between">
        <h2>Upravit Vozidlo</h2>
        <button style="height: 30px; align-self: center;">Uložit</button>
    </div>
    <input type="hidden" name="edit_car" value="1">
    <input type="hidden" name="car_id" value="<?= $car['id'] ?>">
    <div style="display: grid; grid-template-columns: 1fr 1fr; justify-content: center; align-items: center; width: 500px">
        <div>
            <label for="manufacturer">Značka</label>
            <select name="manufacturer" id="manufacturer">
                <?php foreach($manufacturers as $manufacturer):?>
                    <option value="<?= $manufacturer['id'] ?>" <?= $car['id_manufacturer'] == $manufacturer['id'] ? 'selected' : '' ?>><?= $manufacturer['name'] ?></option>
                <?php endforeach?>
            </select>
        </div>
        <div style="display: flex; flex-direction: column; width: 150px">
            <label for="">Model Vozidla</label>
            <input type="text" name="model" value="<?= $car['model'] ?>">
        </div>
        <div style="display: flex; flex-direction: column; width: 150px">
            <label for="">SPZ</label>
            <input type="text" name="license_plate" value="<?= $car['license_plate'] ?>">
        </div>
        <div>
            <label for="employee">Zodpovědný řidič</label>
            <select name="employee" id="employee">
                <?php foreach($employees as $employee):?>
                    <option value="<?= $employee['id'] ?>" <?= $car['id_employee'] == $employee['id'] ? 'selected' : '' ?>><?= $employee['name'] ?></option>
                <?php endforeach?>
            </select>
        </div>
    </div>

</form>
</body>
</html>
Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
747 questions
{count} votes