editar usuario.php


SUBMITTED BY: Lucas312

DATE: Dec. 13, 2016, 12:18 a.m.

FORMAT: PHP

SIZE: 1.2 kB

HITS: 458

  1. <?php
  2. require_once 'init.php';
  3. // resgata os valores do formulário
  4. $name = isset($_POST['name']) ? $_POST['name'] : null;
  5. $email = isset($_POST['email']) ? $_POST['email'] : null;
  6. $gender = isset($_POST['gender']) ? $_POST['gender'] : null;
  7. $birthdate = isset($_POST['birthdate']) ? $_POST['birthdate'] : null;
  8. $id = isset($_POST['id']) ? $_POST['id'] : null;
  9. // validação (bem simples, mais uma vez)
  10. if (empty($name) || empty($email) || empty($gender) || empty($birthdate))
  11. {
  12. echo "Volte e preencha todos os campos";
  13. exit;
  14. }
  15. // a data vem no formato dd/mm/YYYY
  16. // então precisamos converter para YYYY-mm-dd
  17. $isoDate = dateConvert($birthdate);
  18. // atualiza o banco
  19. $PDO = db_connect();
  20. $sql = "UPDATE users SET name = :name, email = :email, gender = :gender, birthdate = :birthdate WHERE id = :id";
  21. $stmt = $PDO->prepare($sql);
  22. $stmt->bindParam(':name', $name);
  23. $stmt->bindParam(':email', $email);
  24. $stmt->bindParam(':gender', $gender);
  25. $stmt->bindParam(':birthdate', $isoDate);
  26. $stmt->bindParam(':id', $id, PDO::PARAM_INT);
  27. if ($stmt->execute())
  28. {
  29. header('Location: index.php');
  30. }
  31. else
  32. {
  33. echo "Erro ao alterar";
  34. print_r($stmt->errorInfo());
  35. }
  36. ?>

comments powered by Disqus