Categories: Development

Creating a PHP database connection file from a form

The ever common PHP MySQL database connection file, mostly named something like dbconnection.php or dbcon.php which gets used in a require_once situation whereby you need a connection to the database.

The code inside this file is either a MySQLi or PDO connection which needs hostname, database name, user and password parameters.

Of course manually filling this out is very easy however you can also use a HTML form and some PHP to create the file.

As you can see below nothing special is used, simple POST from a form with PHP fopen() and fwrite(). You can even choose the name of the output PHP file and the form plus create.php get deleted once you have made the connection file.

Example code

The form (index.html):

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<div class="container">
    <form action="create.php" method="post">
        <div class="form-group row">
            <label for="host" class="col-4 col-form-label">Host</label>
            <div class="col-8">
                <input id="host" name="host" placeholder="localhost" type="text" class="form-control"
                       required="required">
            </div>
        </div>
        <div class="form-group row">
            <label for="db" class="col-4 col-form-label">Database</label>
            <div class="col-8">
                <input id="db" name="db" placeholder="database name" type="text" class="form-control"
                       required="required">
            </div>
        </div>
        <div class="form-group row">
            <label for="user" class="col-4 col-form-label">Username</label>
            <div class="col-8">
                <input id="user" name="user" placeholder="User" type="text" class="form-control" required="required">
            </div>
        </div>
        <div class="form-group row">
            <label for="password" class="col-4 col-form-label">Password</label>
            <div class="col-8">
                <input id="password" name="password" type="text" class="form-control" required="required">
            </div>
        </div>
        <div class="form-group row">
            <label for="filename" class="col-4 col-form-label">File name</label>
            <div class="col-8">
                <input id="filename" name="filename" placeholder="dbconnect" type="text" class="form-control"
                       required="required">
            </div>
        </div>
        <div class="form-group row">
            <div class="offset-4 col-8">
                <button name="submit" type="submit" class="btn btn-primary">Create</button>
            </div>
        </div>
    </form>
</div>

create.php:

<?php
function create_db_connection($hostname, $database, $user, $password, $filename)
{
    $content = '<?php
$db = new PDO("mysql:host=' . $hostname . ';dbname=' . $database . ';charset=utf8mb4", "' . $user . '", "' . $password . '");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);';
    $the_file = fopen("$filename.php", "w") or die("Unable to open file!");
    fwrite($the_file, $content);
    fclose($the_file);
}

create_db_connection($_POST['host'], $_POST['db'], $_POST['user'], $_POST['password'], $_POST['filename']);

unlink('index.html');//Deletes
unlink('create.php');

 

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago