Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include('Conexion.php');
- $pdoData = array_filter((function($p) {
- $rut = $nombre = $edad = $fechaNacimiento = $nacionalidad =
- $sexo = $profesion = '';
- extract($p, EXTR_IF_EXISTS);
- return [
- 'rut'=>$rut, 'nombre'=>$nombre, 'edad'=>$edad, 'profesion'=> $profesion,
- 'sexo'=> ($sexo == 'male') ? 1 : 0, 'nacionalidad'=>$nacionalidad,
- 'fechaNacimiento'=> date('Y-m-d', strtotime($fechaNacimiento)),
- ];
- })($_POST), function($v) { return strlen($v);});
- $required = array_flip(['rut', 'nombre', 'edad', 'nacionalidad', 'fechaNacimiento']);
- if (count(array_diff_key($pdoData, $required))) {
- // a required field is missing; stop processing here and do something
- // reasonable to let the user fix it; note that "die()" is not a good
- // example of "something reasonable"; I include it here only to show
- // that the expectation of the following code is that all required
- // data is available.
- die("Required fields missing");
- }
- try {
- $sql = 'INSERT INTO usuario(rut, nombre, edad, fechaNacimiento, sexo, nacionalidad, id_profesion)
- values(:rut, :nombre, :edad, :fechaNacimiento, :sexo, :nacionalidad, :id_profesion)';
- $stmt = $pdo->prepare($sql);
- $stmt->execute($pdoData);
- } catch (\PDOException $e) {
- // again, you need to do something REASONABLE here; don't just vomit out
- // errors - actually show useful data to the end user about what went
- // wrong and help them fix it.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement