· 8 years ago · Dec 13, 2017, 09:00 PM
1<?php
2class Create_database
3{
4 protected $pdo;
5 protected $name= $_POST['name'];
6 function __construct()
7 {
8 $this->pdo = new PDO("mysql:host=localhost", "usuario", "clave");
9 }
10 //creamos la base de datos y las tablas que necesitemos
11 public function my_db()
12 {
13 //creamos la base de datos si no existe
14 $crear_db = $this->pdo->prepare('CREATE DATABASE IF NOT EXISTS ' . $this->name . ' COLLATE utf8_spanish_ci');
15 $crear_db->execute();
16
17 //decimos que queremos usar la tabla que acabamos de crear
18 if($crear_db):
19 $use_db = $this->pdo->prepare('USE $this->nueva');
20 $use_db->execute();
21 endif;
22
23 //si se ha creado la base de datos y estamos en uso de ella creamos las tablas
24 if($use_db):
25 //creamos la tabla usuarios
26 $crear_tb_users = $this->pdo->prepare('
27 CREATE TABLE IF NOT EXISTS Info (
28 id int(11) NOT NULL AUTO_INCREMENT,
29 nombre varchar(100) COLLATE utf8_spanish_ci NOT NULL,
30 apellido varchar(150) COLLATE utf8_spanish_ci NOT NULL,
31 direccion varchar(150) COLLATE utf8_spanish_ci NOT NULL,
32 pais varchar(150) COLLATE utf8_spanish_ci NOT NULL,
33 ip varchar(150) COLLATE utf8_spanish_ci NOT NULL,
34 telefono varchar(100) COLLATE utf8_spanish_ci NOT NULL,
35 contrasena varchar(100) COLLATE utf8_spanish_ci NOT NULL,
36 registro date NOT NULL,
37 PRIMARY KEY (id)
38 )');
39 $crear_tb_users->execute();
40
41 //creamos la tabla posts
42 $crear_tb_posts = $this->pdo->prepare('
43 CREATE TABLE IF NOT EXISTS Compras (
44 factura int(11) NOT NULL AUTO_INCREMENT,
45 referencia int(11) NOT NULL,
46 email varchar(255) COLLATE utf8_spanish_ci NOT NULL,
47 nombre varchar(255) COLLATE utf8_spanish_ci NOT NULL,
48 valor varchar(255) COLLATE utf8_spanish_ci NOT NULL,
49 ip varchar(255) COLLATE utf8_spanish_ci NOT NULL,
50 productos text COLLATE utf8_spanish_ci NOT NULL,
51 metodo de pago text COLLATE utf8_spanish_ci NOT NULL,
52 fecha datetime NOT NULL,
53 dirección datetime NOT NULL,
54 telefono datetime NOT NULL,
55
56 PRIMARY KEY (id)
57 )');
58 $crear_tb_posts->execute();
59 endif;
60
61 }
62}
63//ejecutamos la función my_db para crear nuestra bd y las tablas
64$db = new Create_database();
65$db->my_db();
66?>