安装PHP for Database

时间:2015-10-12 18:53:07

标签: php mysql post

我正在尝试创建一个安装文件,这样当用户使用我的脚本时,他们可以将他们的凭据输入到多个表单中,然后将该信息发布到数据库文件中等。

下面是安装文件的代码:

<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: admin@gmail.com</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>

然后是db.php文件

<? 
/*  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.");
?>

但是它说它无法连接到数据库。

谢谢, 标记

1 个答案:

答案 0 :(得分:1)

您将这些变量用作字符串。从这些变量中删除“”。

只需按以下代码替换$connection变量:

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

还可以通过以下方式替换$db变量:

$db = mysql_select_db($database_name)

所以整个代码是 -

$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.");
  

注意mysql_pconnect()和mysql_select_db()函数是   弃用。您可以使用mysqli_connect()mysqli_select_db()代替   那些功能。 MySQLi是一个改进版本。

像这样使用 -

    $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.");