Instalador de base de datos en php

Esto es algo que me pidieron en mi universidad, realmente consiste en programar un instalador de una base de datos desde el php, pero con una base de datos ya creada, es decir ya tiene con registros es mucha mas fácil los datos estarán "quemados" en el código php.

Asi que pues una buena forma de hacerlo si ya tenia una base de datos es exportar el sql y lugo copiar cada sentencia en codigo php para convertirlo en un query's, asi que pues en resumidas cuentas todo es un formulario en html y en un post envio los datos de la conexión(host,username,passwords,db).

Como pues serán muchos querys realmente lo mejor sera meterlos a un array y luego recorrer ese array para que en cada iteracion haga query correspondiente.


<html>
<head><title>Instalacion De Base de Datos</title></head>
<body>
<form action="" method="post">
<div align="center">
<p>Este es el Archivo de Instalacion de la base de datos para un Almacen</p>
</div>
<div align="center">
<div>
Host:<input type="text" name="host">
</div>
<div>
Usuario:<input type="text" name="user">
</div>
<div>
Password:<input type="text" name="pass">
</div>
<div>
Nombre DB:<input type="text" name="dbname">
</div>
<div>
<button type="submit">Instalar</button>
</div>
</div>
</form>
</body> 
</html>

<?php
//Visitar ->blog.furiosojack.com
if(isset($_POST['host']) && isset($_POST['user']) && isset($_POST['pass']) && isset($_POST['dbname'])){
 echo "entre";
 $conexion = new mysqli($_POST['host'], $_POST['user'], $_POST['pass']);
 $query = array();
 array_push($query, "CREATE DATABASE `".$_POST['dbname']."` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci");
 array_push($query,"USE `".$_POST['dbname']."`");
 array_push($query,"CREATE TABLE IF NOT EXISTS `almacenes` (`cif` int(11) NOT NULL,`nombre` varchar(50) NOT NULL, `ciudad` varchar(50) NOT NULL,PRIMARY KEY (`cif`))"); //Crea la tabala almacenes
 array_push($query, "INSERT INTO `almacenes` (`cif`, `nombre`, `ciudad`) VALUES(1, 'El Corte Inglés', 'Sevilla'),(2, 'El Corte Inglés', 'Madrid'),(3, 'Jump', 'Valencia'),(4, 'Centro Mail', 'Sevilla'),(5, 'FNAC', 'Barcelona')");
 array_push($query, "CREATE TABLE IF NOT EXISTS `cliente` (  `dni` int(11) NOT NULL,  `nombre` varchar(50) NOT NULL,  `edad` int(11) NOT NULL,  PRIMARY KEY (`dni`))");
 array_push($query,"INSERT INTO `cliente` (`dni`, `nombre`, `edad`) VALUES(1, 'Pepe Perez', 45),(2, 'Juan Gonzalez', 45),(3, 'Maria Gomez', 33),(4, 'Javier Casado', 18),(5, 'Nuria Sanchez', 29),(6, 'Antonio Navarro', 58)");
 array_push($query, "CREATE TABLE IF NOT EXISTS `fabricante` (  `id_fab` int(11) NOT NULL,  `nombre` varchar(50) NOT NULL,  `pais` varchar(30) NOT NULL,  PRIMARY KEY (`id_fab`)) ");
 array_push($query, "INSERT INTO `fabricante` (`id_fab`, `nombre`, `pais`) VALUES (1, 'Oracle ', 'Estados Unidos'),(2, 'Microsoft', 'Estados unidos'),(3, 'IBM', 'Estados unidos'),(4, 'Dinamic', 'España'),(5, 'Borland', 'Estados unidos'),(6, 'Symantec', 'Estados unidos')");
 array_push($query, "CREATE TABLE IF NOT EXISTS `programa` (  `codigo` int(11) NOT NULL,  `nombre` varchar(50) NOT NULL,  `version` varchar(50) NOT NULL,  PRIMARY KEY (`codigo`))");
 array_push($query, "INSERT INTO `programa` (`codigo`, `nombre`, `version`) VALUES(1, 'Application Server', '9i'),(2, 'Database', ' 8i'),(3, 'Database ', '9i'),(4, 'Database ', '10g'),(5, 'Developer', ' 6i'),(6, 'Access ', '97'),(7, 'Access ', '2000'),(8, 'Access ', 'XP'),(9, 'Windows', '98'),(10, 'Windows ', 'XP Professional'),(11, 'Windows ', 'XP Home Edition'),(12, 'Windows ', '2003 Server'),(13, 'Norton Internet Security ', '2004'),(14, 'Freddy Hardest ', '-'),(15, 'Paradox', '2'),(16, 'C++ Builder', '55'),(17, 'DB/2 ', '20'),(18, 'OS/2 ', '10'),(19, 'Jbuilder', ' X'),(20, 'La prisi?n ', '10')");
 array_push($query, "CREATE TABLE IF NOT EXISTS `programa_has_almacenes` (  `programa_codigo` int(11) NOT NULL,  `almacenes_cif` int(11) NOT NULL,  `cantidad` int(11) NOT NULL,  PRIMARY KEY (`programa_codigo`,`almacenes_cif`),  KEY `fk_programa_has_almacenes_almacenes1_idx` (`almacenes_cif`),  KEY `fk_programa_has_almacenes_programa1_idx` (`programa_codigo`)) ");
 array_push($query, "INSERT INTO `programa_has_almacenes` (`programa_codigo`, `almacenes_cif`, `cantidad`) VALUES(1, 1, 10),(1, 2, 6),(2, 1, 11),(2, 2, 6),(6, 1, 5),(6, 2, 4),(7, 1, 3),(7, 2, 7),(8, 5, 8),(10, 1, 5),(10, 3, 8),(13, 1, 7),(13, 3, 5),(14, 4, 3),(15, 5, 8),(16, 5, 2),(17, 5, 3),(19, 5, 6),(20, 4, 6)");
 array_push($query, "CREATE TABLE IF NOT EXISTS `programa_has_fabricante` (  `programa_codigo` int(11) NOT NULL,  `fabricante_id_fab` int(11) NOT NULL,  PRIMARY KEY (`programa_codigo`,`fabricante_id_fab`),  KEY `fk_programa_has_fabricante_fabricante1_idx` (`fabricante_id_fab`),  KEY `fk_programa_has_fabricante_programa1_idx` (`programa_codigo`))");
 array_push($query, "INSERT INTO `programa_has_fabricante` (`programa_codigo`, `fabricante_id_fab`) VALUES(1, 1),(2, 1),(3, 1),(4, 1),(5, 1),(6, 2),(7, 2),(8, 2),(9, 2),(10, 2),(11, 2),(12, 2),(17, 3),(18, 3),(14, 4),(20, 4),(15, 5),(16, 5),(19, 5),(13, 6)");  
 array_push($query, "CREATE TABLE IF NOT EXISTS `registro` (  `medio` varchar(20) NOT NULL,  `codigo` int(11) NOT NULL,  `almacenes_cif` int(11) NOT NULL,  `cliente_dni` int(11) NOT NULL,  KEY `fk_registro_almacenes1_idx` (`almacenes_cif`),  KEY `fk_registro_cliente1_idx` (`cliente_dni`))");
 array_push($query, "INSERT INTO `registro` (`medio`, `codigo`, `almacenes_cif`, `cliente_dni`) VALUES('Internet', 1, 1, 1),('Tarjeta postal', 4, 1, 3),('Tel?fono', 10, 4, 2),('Tarjeta postal', 10, 4, 1),('Internet', 12, 5, 2),('Internet', 15, 2, 4)");   
 ///Querys de realaciones
 array_push($query, "ALTER TABLE `programa_has_almacenes`  ADD CONSTRAINT `fk_programa_has_almacenes_almacenes1` FOREIGN KEY (`almacenes_cif`) REFERENCES `almacenes` (`cif`) ON DELETE NO ACTION ON UPDATE NO ACTION,  ADD CONSTRAINT `fk_programa_has_almacenes_programa1` FOREIGN KEY (`programa_codigo`) REFERENCES `programa` (`codigo`) ON DELETE NO ACTION ON UPDATE NO ACTION");
 array_push($query, "ALTER TABLE `programa_has_fabricante`  ADD CONSTRAINT `programa_has_fabricante_ibfk_1` FOREIGN KEY (`programa_codigo`) REFERENCES `programa` (`codigo`) ON DELETE CASCADE ON UPDATE CASCADE,  ADD CONSTRAINT `programa_has_fabricante_ibfk_2` FOREIGN KEY (`fabricante_id_fab`) REFERENCES `fabricante` (`id_fab`) ON DELETE CASCADE ON UPDATE CASCADE");
 array_push($query, "ALTER TABLE `registro`  ADD CONSTRAINT `fk_registro_almacenes1` FOREIGN KEY (`almacenes_cif`) REFERENCES `almacenes` (`cif`) ON DELETE NO ACTION ON UPDATE NO ACTION,  ADD CONSTRAINT `fk_registro_cliente1` FOREIGN KEY (`cliente_dni`) REFERENCES `cliente` (`dni`) ON DELETE NO ACTION ON UPDATE NO ACTION");
 foreach($query as $miquery){
  $conexion->query($miquery) or die("Error en el QUERY:".mysqli_error($conexion)); 
 }
 //Visitar ->blog.furiosojack.com
 echo "<script>alert('Base de Datos Instalada Correctamente')</script>";
 
}
//Visitar ->blog.furiosojack.com
?>

0 Comentarios