0

基本上我的问题是下一个......下面的脚本连接数据库并创建一个文件......一切正常......除了它填充文件时......它没有将变量$username,$password和放入文件。创建的文件看起来像这样$server$dbname

<?php

    // Database Constants
    $DB_SERVER ="";
    $DB_USER ="";
    $DB_PASS ="";
    $DB_NAME ="";
    ?>

但它应该在引号内有一些东西:S

我的脚本如下

<?php 
//DB Config File
$dbFile = 'dbconfig.php';

function createfile ($dbFile) {
        //Creates File and populates it.
        $fOpen = fopen($dbFile, 'w');

            $fString .= "<?php\n";
            $fString .= "// Database Constants\n";
            $fString .= "\$DB_SERVER =" . "\"" . $server . "\";\n";
            $fString .= "\$DB_USER =" . "\"" . $username . "\";\n";
            $fString .= "\$DB_PASS =" . "\"" . $password . "\";\n";
            $fString .= "\$DB_NAME =". "\"" . $dbname . "\";\n";
            $fString .= "?>";

        fwrite($fOpen, $fString);
        fclose($fOpen);
return true;
}


if (isset($_POST['submit'])) {

$username = $_POST['username'];
$password = $_POST['password'];
$server = $_POST['server'];
$dbname = $_POST['dbname'];


try {
$db = new PDO ('mysql:host=' .$server.';dbname='.$dbname,$username,$password);

if ($db) { //if succesful at connecting to the DB

if (file_exists($dbFile)){
    if (is_readable($dbFile) && is_writable($dbFile)){ 

        //Creates File, populates it and redirects the user

    if (createfile($dbFile)) { 
    header("Location: http://http://localhost/proj11/install2.php");
    exit ();
            }


        } else { 

        $msg = "2The file {$dbFile} cannot be accessed. Please configure the file manualy or grant Write and Read permission.";  }

    } else {

        //Creates File, populates it and redirects the user

    if (createfile($dbFile)) {

    header("Location: http://http://localhost/proj11/install2.php");
    exit ();
            }

        }


}

} catch (PDOException $e) { //Catchs error if can't connect to the db.
    $msg = 'Connection failed: ' . $e->getMessage();
}


}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>

<body>
<form id="iForm" method="post" action="install.php">
<label id="username" >Username</label>
<input id="username" name="username"/>
<label id="password">Password</label>
<input id="password" name="password" />
<label id="server" >Server</label>
<input id="server" name="server"/>
<label id="dbName" >dbName</label>
<input id="dbName" name="dbname"/>

<input type="submit" name="submit" value="submit" />
</form>
<p id="error"><?php echo $msg ?></p>
</body>
</html>
4

4 回答 4

3

您的变量$username, $password, ... 不是全局变量。它们不在函数的范围内。您需要将这些变量传递给函数。

或使用global关键字:

function createfile ($dbFile) {
  global $server;
  global $username;
  global $password;
  global $dbname;
  ...

但请注意,过度使用global是非常糟糕的做法。

于 2012-08-23T16:20:47.157 回答
1

你没有传递$server, $username, $password, $dbname给函数。

尝试做

function createfile($dbfile,$params)
{
   list($server,$username,$password,$dbname) = $params;
   /* ... */
}

/* ... */

if (createfile($dbFile,array($server,$username,$pasword,$dbname)))
于 2012-08-23T16:22:07.517 回答
1

你需要转换:

function createfile ($dbFile) {
        //Creates File and populates it.

对此:

function createfile ($dbFile) {
        global $username, $password, $server, $dbname; 
        //Creates File and populates it.

或这个 :

function createfile ($dbFile, $username, $password, $server, $dbname) {
        //Creates File and populates it.

在最后一种情况下,您的电话自然会变成:

createfile ($dbFile, $username, $password, $server, $dbname);
于 2012-08-23T16:22:52.563 回答
0

您要么需要使用global关键字引用全局变量:

function createfile ($dbFile) {
    global $username, $password, $server, $dbname;

或将它们createfile显式传递给:

function createfile ($dbFile, $username, $password, $server, $dbname) {

/* ... */

if (createfile($dbFile, $username, $password, $server, $dbname)) { 
于 2012-08-23T16:22:53.753 回答