Mark Barrett
Mark Barrett

Reputation: 436

Installation PHP for Database

I am trying to make an installation file, so that when a user uses my script, they can enter their credentials into a number of forms and it goes and posts that information in the database file etc.

Heres my code for the installation file:

<html>

<head>
    <title>Installation</title>
        <link href="/css/bootstrap.min.css" rel="stylesheet" media="screen">

</head>
    <div class="container">
        <h2>Welcome to McApplicator!</h2>
            <p>Installation is simple. Please follow the instructions below.</p>

            <?php

                        $dbhost = $_POST['database_server'];
                        $dbusername= $_POST['database_user'];
                        $dbpasswd= $_POST['database_password'];
                        $database_name= $_POST['database_name'];  
                        $owner_email = $_POST['owner_email'];


            ?>
    <form action="register.php" method="post" name="" id="">

        <div class="form-group">
                            <label>Database Server</label>
                            <input type="text" class="form-control" name="database_server" value="localhost" />
                        </div>
                        <div class="form-group">
                            <label>Database User</label>
                            <input type="text" class="form-control" name="database_user" />
                        </div>
                        <div class="form-group">
                            <label>Database Password</label>
                            <input type="text" class="form-control" name="database_password" />
                        </div>
                        <div class="form-group">
                            <label>Database Name</label>
                            <input type="text" class="form-control" name="database_name" />
                        </div>

                    <div class="form-group">
                            <label>Owners Email</label>
                            <p class="help-block">e.g: [email protected]</p>
                            <input type="text" class="form-control" name="owner_email" />
                        </div>

                        <div class="form-group">
                            <button type="submit" name="submit" class="btn btn-primary col-lg-4">Install</button>
                        </div>
                    </form>
</html>

and then the db.php file

<? 
/*  Database Information - Required!!  */
/* -- Configure the Variables Below --*/
  $dbhost = $_POST['database_server'];
$dbusername= $_POST['database_user'];
$dbpasswd= $_POST['database_password'];
$database_name= $_POST['database_name'];  

/* Database Stuff, do not modify below this line */

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") 
    or die ("Couldn't connect to server.");

$db = mysql_select_db("$database_name", $connection)
    or die("Couldn't select database.");
?>

However it says it cannot connect to the database.

Thanks, Mark

Upvotes: 0

Views: 55

Answers (1)

Md. Shamim Shahnewaz
Md. Shamim Shahnewaz

Reputation: 114

You used that variables as an string. Remove the "" from those variables.

Just replace the $connection variable by following:

$connection = mysql_pconnect($dbhost,$dbusername,$dbpasswd)

And also replace $db variable by following:

$db = mysql_select_db($database_name)

So the whole code is -

$dbhost = $_POST['database_server'];
$dbusername= $_POST['database_user'];
$dbpasswd= $_POST['database_password'];
$database_name= $_POST['database_name'];

/* Database Stuff, do not modify below this line */

$connection = mysql_pconnect($dbhost,$dbusername,$dbpasswd)
or die ("Couldn't connect to server.");

$db = mysql_select_db($database_name)
or die("Couldn't select database.");

Note that mysql_pconnect() and mysql_select_db() functions are deprecated. You can use mysqli_connect() mysqli_select_db() instead of those functions. MySQLi is an improved version.

Use Like this -

    $dbhost = $_POST['database_server'];
    $dbusername= $_POST['database_user'];
    $dbpasswd= $_POST['database_password'];
    $database_name= $_POST['database_name'];

    /* Database Stuff, do not modify below this line */

    $connection = mysqli_connect($dbhost,$dbusername,$dbpasswd)
    or die ("Couldn't connect to server.");

    $db = mysqli_select_db($database_name)
    or die("Couldn't select database.");

Upvotes: 1

Related Questions