Advertisement
Guest User

Untitled

a guest
May 30th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. <?php
  2. include('Conexion.php');
  3. $pdoData = array_filter((function($p) {
  4. $rut = $nombre = $edad = $fechaNacimiento = $nacionalidad =
  5. $sexo = $profesion = '';
  6. extract($p, EXTR_IF_EXISTS);
  7. return [
  8. 'rut'=>$rut, 'nombre'=>$nombre, 'edad'=>$edad, 'profesion'=> $profesion,
  9. 'sexo'=> ($sexo == 'male') ? 1 : 0, 'nacionalidad'=>$nacionalidad,
  10. 'fechaNacimiento'=> date('Y-m-d', strtotime($fechaNacimiento)),
  11. ];
  12. })($_POST), function($v) { return strlen($v);});
  13. $required = array_flip(['rut', 'nombre', 'edad', 'nacionalidad', 'fechaNacimiento']);
  14. if (count(array_diff_key($pdoData, $required))) {
  15. // a required field is missing; stop processing here and do something
  16. // reasonable to let the user fix it; note that "die()" is not a good
  17. // example of "something reasonable"; I include it here only to show
  18. // that the expectation of the following code is that all required
  19. // data is available.
  20. die("Required fields missing");
  21. }
  22.  
  23. try {
  24. $sql = 'INSERT INTO usuario(rut, nombre, edad, fechaNacimiento, sexo, nacionalidad, id_profesion)
  25. values(:rut, :nombre, :edad, :fechaNacimiento, :sexo, :nacionalidad, :id_profesion)';
  26.  
  27. $stmt = $pdo->prepare($sql);
  28. $stmt->execute($pdoData);
  29. } catch (\PDOException $e) {
  30. // again, you need to do something REASONABLE here; don't just vomit out
  31. // errors - actually show useful data to the end user about what went
  32. // wrong and help them fix it.
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement