Guest User

Untitled

a guest
Dec 13th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <?php
  2. class Create_database
  3. {
  4. protected $pdo;
  5. protected $name;
  6. function __construct()
  7. {
  8. $this->pdo = new PDO("mysql:host=localhost;", "usuario", "contraseña");
  9. $this->name=$_POST['name'];
  10. }
  11. //creamos la base de datos y las tablas que necesitemos
  12. public function my_db()
  13. {
  14. //creamos la base de datos si no existe
  15. $crear_db = $this->pdo->prepare('CREATE DATABASE IF NOT EXISTS ' . $this->name . ' COLLATE utf8_spanish_ci');
  16. $crear_db->execute();
  17.  
  18. //decimos que queremos usar la tabla que acabamos de crear
  19. if($crear_db):
  20. $use_db = $this->pdo->prepare('USE $this->name');
  21. $use_db->execute();
  22. endif;
  23.  
  24. //si se ha creado la base de datos y estamos en uso de ella creamos las tablas
  25. if($use_db):
  26. //creamos la tabla usuarios
  27. $crear_tb_users = $this->pdo->prepare('
  28. CREATE TABLE IF NOT EXISTS Info (
  29. id int(11) NOT NULL AUTO_INCREMENT,
  30. nombre varchar(100) COLLATE utf8_spanish_ci NOT NULL,
  31. apellido varchar(150) COLLATE utf8_spanish_ci NOT NULL,
  32. direccion varchar(150) COLLATE utf8_spanish_ci NOT NULL,
  33. pais varchar(150) COLLATE utf8_spanish_ci NOT NULL,
  34. ip varchar(150) COLLATE utf8_spanish_ci NOT NULL,
  35. telefono varchar(100) COLLATE utf8_spanish_ci NOT NULL,
  36. contrasena varchar(100) COLLATE utf8_spanish_ci NOT NULL,
  37. registro date NOT NULL,
  38. PRIMARY KEY (id)
  39. )');
  40. $crear_tb_users->execute();
  41.  
  42. //creamos la tabla posts
  43. $crear_tb_posts = $this->pdo->prepare('
  44. CREATE TABLE IF NOT EXISTS Compras (
  45. factura int(11) NOT NULL AUTO_INCREMENT,
  46. referencia int(11) NOT NULL,
  47. email varchar(255) COLLATE utf8_spanish_ci NOT NULL,
  48. nombre varchar(255) COLLATE utf8_spanish_ci NOT NULL,
  49. valor varchar(255) COLLATE utf8_spanish_ci NOT NULL,
  50. ip varchar(255) COLLATE utf8_spanish_ci NOT NULL,
  51. productos text COLLATE utf8_spanish_ci NOT NULL,
  52. metodo de pago text COLLATE utf8_spanish_ci NOT NULL,
  53. fecha datetime NOT NULL,
  54. dirección datetime NOT NULL,
  55. telefono datetime NOT NULL,
  56.  
  57. PRIMARY KEY (id)
  58. )');
  59. $crear_tb_posts->execute();
  60. endif;
  61.  
  62. }
  63. }
  64. //ejecutamos la función my_db para crear nuestra bd y las tablas
  65. $db = new Create_database();
  66. $db->my_db();
  67. if($db->my_db()){
  68. echo "se creo la base de datos";
  69. }
  70. else{
  71. echo "no se creo la base de datos";
  72. }
  73. ?>
Add Comment
Please, Sign In to add comment