I am helping a friend build as site and at a testing stage. it came to creating a database and i discovered he was a bity lost. i explained howto do it in phpadmin but i then decided to write a page or 2 to install it would be a nice project to do.
I got it done ( although messy) and would like to make it more generic for possible future use. So im risking your comments in the hope i can learn a bit more.
Ok first page 1
connects to sever (PDO )
creates database and a config file . so far so good
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<style>
label{ width:150px; float: left; clear:left;}
input{width:200px; float: left;}
</style><body>
<form action="" method="post">
Connect to server<br>
<label>Host name</label><input name="host" type="text"><br>
<label>user</label><input name="user" type="text"><br>
<label>Server pass word</label><input name="pass" type="text"><br>
<br>
<br>
New database and user <br>
<label>Database</label><input name="datab" type="text"><br>
<label>New user</label><input name="n_user" type="text"><br>
<label>New password</label><input name="n_pass" type="text"><br>
<label>create database</label><input name="go" type="submit" value="Submit"></form>
</body>
</html>
<?php
$host=$_POST['host'];
$root=$_POST['user'];
$root_password=$_POST['pass'];
$user=$_POST['n_user'];
$pass=$_POST['n_pass'];
$db=$_POST['datab'];
$dbh = new PDO("mysql:host=$host", $root, $root_password);
$dbh->exec("CREATE DATABASE `$db`;
CREATE USER '$user'@'$host' IDENTIFIED BY '$pass';
GRANT ALL ON `$db`.* TO '$user'@'$host';
FLUSH PRIVILEGES;") ;
$data = " <?php ".
'$host'.'='."\"".$host."\"". ";\n\r".
'$uhost'.'='."\"".$db."\"". ";\n\r".
'$uuser' .'='."\"". $user."\"". ";\n\r".
'$upass' .'='."\"".$pass."\"". ";\n\r".
';?>' ;
$ret = file_put_contents('conn.php', $data);
?>
<br>
<a href="step2.php">go</a></body>
file 2 uses the config file i created for the new connection,
where i have the exec)....create table
The second file ands a table and some dummy data.
I would like to perhaps search for a file so $_POST...whatever added to a form. Is this possible? i have searched but cant find a reference.
its basic and does the job but would be nice to expand. maybe check connection before submitting form.
Also this is for a testing server however it occurs that live it would name a database and user as [SERVER]_database
and [SERVER]_user, and im not sure how to approach that. Obviously a bit out of my depth but trying to swim :)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
include 'conn.php';
try {
$dbh= new PDO("mysql:host=localhost;dbname=$uhost",$uuser,$upass);
$dbh->exec("
CREATE TABLE IF NOT EXISTS `item` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NULL,
`country` varchar(100)NULL,
`proof` varchar(100) NULL,
`age` varchar(100) NULL,
`type` varchar(100) NULL,
`grade` varchar(100) NULL,
`description` mediumtext NULL,
`flavour` varchar(100) NULL,
`image` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
INSERT INTO `item` (`id`, `name`, `country`, `proof`, `age`, `type`, `grade`, `description`, `flavour`, `image`) VALUES
(1, 'yingyang', 'japan', '70', '1', 'any', '60', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'camel piss', 'images/bottle.jpg'),
(2, 'tartan lion', 'thailand', '40', '2', 'any', '30', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam auctor lorem pharetra lacus convallis commodo eu iaculis lectus. Suspendisse quis eros varius, consequat arcu vel, fermentum nisl.', 'chemical soup', 'images/bottle.jpg'),
(3, 'island wee Dram', 'scotland', '40', '25', 'single', '40', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam auctor lorem pharetra lacus convallis commodo eu iaculis lectus. Suspendisse quis eros varius, consequat arcu vel, fermentum nisl. Vestibulum accumsan scelerisque quam.Donec at lacus eget quam es porttitor turpis eu, cursus molestie metus.', 'peat', 'images/bottle.jpg'),
(4, 'Pog Mahon', 'scotland', '42', '3', 'blended', '90', ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam auctor lorem pharetra lacus convallis commodo eu iaculis lectus. Suspendisse quis eros varius, consequat arcu vel, fermentum nisl. ', 'coek', 'images/bottle.jpg');
");
} catch (PDOException $e) {
die("DB ERROR: ". $e->getMessage());
}