registration.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. require $_SERVER['DOCUMENT_ROOT'].'/services/connect.php'; //Подключение connect.php
  3. $_POST = json_decode(file_get_contents('php://input'), true);
  4. //Пост запрос к БД на вставку
  5. $name = $_POST["name"];
  6. $surname = $_POST["surname"];
  7. $address = $_POST["address"];
  8. $email = $_POST["email"];
  9. $password = $_POST["password"];
  10. //Хешируем пароль, чтобы безопасно сохранить
  11. $hash = password_hash($password, PASSWORD_DEFAULT);
  12. $query = "INSERT INTO users (`name`, `surname`, `address`, `email`, `password`) VALUES ('$name', '$surname', '$address', '$email', '$hash')";
  13. $result = $mysql->query($query);
  14. if(!$result){
  15. echo "Произошли неполадки при регистрации: ". $mysql->error;
  16. }
  17. //Response
  18. $query = "SELECT * FROM users WHERE `email` = '$email'";
  19. $result = $mysql->query($query);
  20. if(!$result){
  21. echo "Произошли неполадки при получении email: ". $mysql->error;
  22. }
  23. $id = $result->fetch_assoc()["id"];
  24. $data = [
  25. "id" => $id,
  26. "name" => $name,
  27. "surname" => $surname,
  28. "address" => $address,
  29. "email" => $email,
  30. "result" => true
  31. ];
  32. header('Content-Type: application/json;charset=utf-8');
  33. echo json_encode($data);