Skip to content

Instantly share code, notes, and snippets.

@bitforth
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitforth/3017f2a7de4e06cc5c52 to your computer and use it in GitHub Desktop.
Save bitforth/3017f2a7de4e06cc5c52 to your computer and use it in GitHub Desktop.
<?php
require_once('conexion.php'); // Definicion de bases de datos y demas boberias
$nombre = substr($_POST['nombre'], 0, 49); //Truncar la cadena de texto a los primeros 50 caracteres.
$nombre - filter_var($nombre, FILTER_SANITIZE_SPECIAL_CHARS); // Codificar caracteres HTML.
$telefono = string_replace(array('+','-','.'), $_POST['telefono']);// Eliminamos caracteres especiales del telefono
$telefono = filter_var($telefono, FILTER_SANITIZE_NUMBER_INT); // Nos aseguramos que solamente queden numeros en la cadena de texto
$telefono = substr($telefono, -10); // Seleccionamos los ultimos 10 caracteres del numero, ignorando el codigo del pais.
$insertQuery = "INSERT INTO Contactos(nombre, telefono) VALUES(:nombre, :telefono)";
try {
$stmt = $pdo->prepare($insertQuery);
$stmt->bindParam(':nombre', $nombre, PDO::PARAM_STR);
$stmt->bindParam(':telefono', $telefono, PDO::PARAM_STR);// los numeros de telefono generalmente se almacenan como char en las bases de datos para evitar int eger overflow
$stmt->execute();
} catch (Exception $e) {
// haz lo que tengas que hacer con la excepcion si es que ocurre.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment