Advertisement
sanjiisan

Untitled

Apr 13th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. *
  5. * Konfiguracja bazy danych
  6. */
  7. $host = "localhost";
  8. $user = "root";
  9. $pass = "coderslab";
  10. $db = "products_ex";
  11.  
  12. if ($_SERVER['REQUEST_METHOD'] === 'POST') {//Odebranie danych z formularza
  13. if (isset($_POST['name']) && isset($_POST['description']) && isset($_POST['price'])) {
  14.  
  15. //Przygotowanie danych
  16. $name = trim($_POST['name']);
  17. $description = trim($_POST['description']);
  18. $price = trim($_POST['price']);
  19.  
  20. try {
  21. $conn = new PDO( //Łączenie sie z bazą danych
  22. "mysql:host=$host;dbname=$db;charset=UTF8",
  23. $user, //root
  24. $pass,//coderslab
  25. [
  26. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  27. ]
  28. );
  29.  
  30. $sql = "INSERT INTO products(name, description, price)
  31. VALUES (:name, :description, :price)"; //Piszemy zapytanie do bazy danych które wrzuci do niej elementy
  32.  
  33. $stmt = $conn->prepare($sql);//Sprawdzenie zapytania
  34.  
  35. $stmt->execute([ //Wykonanie i wstawienie konretnych danych
  36. 'name' => $name,
  37. 'description' => $description,
  38. 'price' => $price
  39. ]);
  40.  
  41. echo 'Nowy element w bazie';
  42.  
  43. } catch (PDOException $e) { //Odebranie błędu i komunikat
  44. echo $e->getMessage();
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement