Advertisement
sanjiisan

Untitled

Apr 13th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. <?php
  2.  
  3. $host = "localhost";
  4. $user = "root";
  5. $pass = "coderslab";
  6. $db = "cinemas_ex"; //nazwa bazy
  7.  
  8. $conn = new PDO(
  9. "mysql:host=$host;dbname=$db;charset=UTF8",
  10. $user, //root
  11. $pass,//coderslab
  12. [
  13. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  14. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  15. ]
  16. );
  17.  
  18. //poniżej napisz kod pobierający dane o pojedynczym filmie po przesłaniu żądania GET
  19. if ($_SERVER['REQUEST_METHOD'] === 'GET') {
  20. if (isset($_GET['id']) && is_numeric($_GET['id'])) {
  21.  
  22. $sql = 'SELECT * FROM Movies WHERE id=:id;'; //Pobranie konkretnego elementu
  23. $stmt = $conn->prepare($sql); /** Korzystamy z prepare bo mamy dane dostawane od uzytkownika */
  24. $stmt->execute([
  25. 'id' => $_GET['id']
  26. ]); //poddstawiamy pod placeholdery konkretne dane
  27.  
  28. $movie = $stmt->fetch();//Przypisze pierwszy element zwrocony z zapytania
  29.  
  30. $id = $movie['id']; //wyciągamy poszczegolne dane o całym filmie
  31. $name = $movie['name'];
  32. $description = $movie['description'];
  33. $rating = $movie['rating'];
  34. }
  35. }
  36.  
  37.  
  38. //obsluga formualrza updatujacego
  39. if ($_SERVER['REQUEST_METHOD'] === 'POST') { //sprawdzamy czy postem
  40.  
  41. if (isset($_POST['movieId']) && isset($_POST['movieName']) && isset($_POST['movieDescription']) && isset($_POST['movieRating'])) { //sprawdzamy czy wszystkie dane sa wysyłane
  42.  
  43. $id = $_POST['movieId']; //wyciągamy poszczegolne dane o całym filmie
  44. $name = $_POST['movieName'];
  45. $description = $_POST['movieDescription'];
  46. $rating = $_POST['movieRating'];
  47.  
  48. //To zapytanie updatejtuje dany rekord w bazie danych
  49. $sql = "UPDATE Movies SET name=:name, description=:description, rating=:rating WHERE id=:id";
  50. $stmt = $conn->prepare($sql);
  51.  
  52. $stmt->execute([ //przekazujemy do zapytania konkretne argumenty
  53. 'id' => $id,
  54. 'name' => $name,
  55. 'description' => $description,
  56. 'rating' => $rating,
  57. ]);
  58. }
  59. }
  60.  
  61. ?>
  62. <!doctype html>
  63. <html lang="en">
  64. <head>
  65. <meta charset="UTF-8">
  66. <meta name="viewport"
  67. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  68. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  69. <title>Zadanie 1 - edycja filmu</title>
  70. <!-- Latest compiled and minified CSS -->
  71. <link rel="stylesheet" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  72. </head>
  73. <body>
  74. <div class="container">
  75. <div class="row">
  76. <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
  77.  
  78. </div>
  79. <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
  80. <form action="" method="post" role="form">
  81. <legend>Edycja filmu</legend>
  82. <div class="form-group">
  83. <label for="">Id filmu</label>
  84. <input type="text" class="form-control" readonly name="movieId" id="movieId" placeholder="Id filmu"
  85. value="<?= $id ?>">
  86. </div>
  87. <div class="form-group">
  88. <label for="">Nazwa filmu</label>
  89. <input type="text" class="form-control" name="movieName" id="movieName" placeholder="Nazwa filmu"
  90. value="<?= $name ?>">
  91. </div>
  92. <div class="form-group">
  93. <label for="">Opis filmu</label>
  94. <input type="text" class="form-control" name="movieDescription" id="movieDescription"
  95. placeholder="Opis filmu"
  96. value="<?= $description ?>">
  97. </div>
  98. <div class="form-group">
  99. <label for="">Rating filmu</label>
  100. <input type="number" step="0.01" class="form-control" name="movieRating" id="movieRating"
  101. placeholder="Rating filmu"
  102. value="<?= $rating ?>">
  103. </div>
  104. <button type="submit" class="btn btn-primary">Edytuj</button>
  105. </form>
  106. </div>
  107. <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
  108.  
  109. </div>
  110. </div>
  111. </div>
  112. </body>
  113. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement