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…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago