Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = "localhost";
- $user = "root";
- $pass = "coderslab";
- $db = "cinemas_ex"; //nazwa bazy
- $conn = new PDO(
- "mysql:host=$host;dbname=$db;charset=UTF8",
- $user, //root
- $pass,//coderslab
- [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
- ]
- );
- //poniżej napisz kod pobierający dane o pojedynczym filmie po przesłaniu żądania GET
- if ($_SERVER['REQUEST_METHOD'] === 'GET') {
- if (isset($_GET['id']) && is_numeric($_GET['id'])) {
- $sql = 'SELECT * FROM Movies WHERE id=:id;'; //Pobranie konkretnego elementu
- $stmt = $conn->prepare($sql); /** Korzystamy z prepare bo mamy dane dostawane od uzytkownika */
- $stmt->execute([
- 'id' => $_GET['id']
- ]); //poddstawiamy pod placeholdery konkretne dane
- $movie = $stmt->fetch();//Przypisze pierwszy element zwrocony z zapytania
- $id = $movie['id']; //wyciągamy poszczegolne dane o całym filmie
- $name = $movie['name'];
- $description = $movie['description'];
- $rating = $movie['rating'];
- }
- }
- //obsluga formualrza updatujacego
- if ($_SERVER['REQUEST_METHOD'] === 'POST') { //sprawdzamy czy postem
- if (isset($_POST['movieId']) && isset($_POST['movieName']) && isset($_POST['movieDescription']) && isset($_POST['movieRating'])) { //sprawdzamy czy wszystkie dane sa wysyłane
- $id = $_POST['movieId']; //wyciągamy poszczegolne dane o całym filmie
- $name = $_POST['movieName'];
- $description = $_POST['movieDescription'];
- $rating = $_POST['movieRating'];
- //To zapytanie updatejtuje dany rekord w bazie danych
- $sql = "UPDATE Movies SET name=:name, description=:description, rating=:rating WHERE id=:id";
- $stmt = $conn->prepare($sql);
- $stmt->execute([ //przekazujemy do zapytania konkretne argumenty
- 'id' => $id,
- 'name' => $name,
- 'description' => $description,
- 'rating' => $rating,
- ]);
- }
- }
- ?>
- <!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">
- <title>Zadanie 1 - edycja filmu</title>
- <!-- Latest compiled and minified CSS -->
- <link rel="stylesheet" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
- </head>
- <body>
- <div class="container">
- <div class="row">
- <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
- </div>
- <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
- <form action="" method="post" role="form">
- <legend>Edycja filmu</legend>
- <div class="form-group">
- <label for="">Id filmu</label>
- <input type="text" class="form-control" readonly name="movieId" id="movieId" placeholder="Id filmu"
- value="<?= $id ?>">
- </div>
- <div class="form-group">
- <label for="">Nazwa filmu</label>
- <input type="text" class="form-control" name="movieName" id="movieName" placeholder="Nazwa filmu"
- value="<?= $name ?>">
- </div>
- <div class="form-group">
- <label for="">Opis filmu</label>
- <input type="text" class="form-control" name="movieDescription" id="movieDescription"
- placeholder="Opis filmu"
- value="<?= $description ?>">
- </div>
- <div class="form-group">
- <label for="">Rating filmu</label>
- <input type="number" step="0.01" class="form-control" name="movieRating" id="movieRating"
- placeholder="Rating filmu"
- value="<?= $rating ?>">
- </div>
- <button type="submit" class="btn btn-primary">Edytuj</button>
- </form>
- </div>
- <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
- </div>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement