基本上我的问题是下一个。。。以下脚本连接数据库并创建一个文件。。。。一切都很好。。除了当它填充文件时。。它不将变量$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>
您的变量$username
、$password
。。。没有全局变量。它们不在功能范围内。您需要将这些变量传递给函数。
或者使用global
关键字:
function createfile ($dbFile) {
global $server;
global $username;
global $password;
global $dbname;
...
但请注意,过度使用global
是非常糟糕的做法。
您需要转换:
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);
您没有将$server, $username, $password, $dbname
传递给函数。
尝试进行
function createfile($dbfile,$params)
{
list($server,$username,$password,$dbname) = $params;
/* ... */
}
/* ... */
if (createfile($dbFile,array($server,$username,$pasword,$dbname)))
您需要使用global
关键字引用全局变量:
function createfile ($dbFile) {
global $username, $password, $server, $dbname;
或者明确地将它们传递给createfile
:
function createfile ($dbFile, $username, $password, $server, $dbname) {
/* ... */
if (createfile($dbFile, $username, $password, $server, $dbname)) {