Jump to content

Database installer


Lamez

Recommended Posts

I am trying to make a database installer, when I mean when I say that is: instead of changing my config.php file manually, I want a php script to do it for me, and automatically add the tables I need, and automatically add an admin user to the database under the admin table, and I want to know how I can go about doing this?

 

I think I can get the tables and adding the user, but how do I go about editing my config file? I need to add variables in my config file.

Is there a way I can use a .sql file to add the tables, and users to the database through php?

any help?

 

-Thanks  :P

Link to comment
Share on other sites

As for creating the config.php file automatically based on the information entered in the configuratioin form you can do this:

<?php

if(isset($_POST['submit']))
{
    $errors = null;
    foreach($_POST['config'] as $key => $value)
    {
        if(isset($_POST['config'][$key]) && empty($_POST['config'][$key]))
        {
            $field = str_replace(array('db', '_'), array('Database', ' '), $key);

            $errors[] = '<b>' . ucwords($field) . '</b> field left blank!';
        }
    }

    if(is_array($errors))
    {
        $error_msg =  "Please correct the following errors:\n<ul>\n  <li>" . implode("</li>\n  <li>", $errors) . "</li>\n</ul>\n\n";
    }
    else
    {
        $config = "<?php\n\n";

        foreach($_POST['config'] as $key => $value)
        {
            $config .= '$'.$key .' = \'' . $value . "';\n";
        }

        $config .= "\n?>";

        // write configuration to config.php
        $handle = fopen('config.php', 'w'); // open
        fwrite($handle, $config);           // write
        fclose($handle);                    // close

        echo 'config.php created:<br /><br />';

        echo '<textarea name="" rows="10" cols="60">' . $config . '</textarea>';
    }

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Database installer</title>
</head>
<body>
<h1>Database Installer</h1>
<?php if(isset($error_msg) && !empty($error_msg)) echo $error_msg; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <fieldset>
    <legend>Database Infomation</legend>
    Hostname: <input type="text" name="config[db_hostname]" value="localhost" /><br />
    Username: <input type="text" name="config[db_username]" /><br />
    Password: <input type="text" name="config[db_password]" /><br />
    Name: <input type="text" name="config[db_name]" />
  </fieldset>
  <br />
  <fieldset>
    <legend>Administrator Infomation</legend>
    Username: <input type="text" name="config[admin_username]" /><br />
    Password: <input type="text" name="config[admin_password]" />
  </fieldset>
  <br />
  <input type="submit" name="submit" value="Setup Database" />
</form>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.