好的,所以我正在测试我的安装脚本,我收到一个数据库不存在的错误,这意味着数据库创建查询失败,
这是我的完整代码:
<?php
if(!file_exists('install.lock')) {
if(!isset($_GET['success'])) {
echo "
<!DOCTYPE HTML>
<head>
<title> Lorem Ipsum Dolor </title>
<link href='../assets/css/style.css' rel='stylesheet'>
<style type='text/css'>
body {
text-align:center;
}
</style>
</head>
<body>
<div id='containerAdmin'>
<br />
<h2> Lorem Ipsum </h2> <br />
<h4> Please follow the steps below to install your new copy of ticketExpress </h4> <br />
<form action='' method='post'>
<input type='text' name='dbhost' placeholder='MySQL Database Host' size='23'>
<input type='text' name='dbuser' placeholder='MySQL Database Username' size='23'>
<input type='text' name='dbpass' placeholder='MySQL Database Password' size='23'>
<br />
<input type='text' name='admin_username' placeholder='Admin Username'>
<input type='text' name='admin_password' placeholder='Admin Password'> <br />
<input type='text' name='sitename' placeholder='Website Name' size='23'> <br />
<input type='submit' name='install' class='btn btn-primary' value='Install'>
</form>
<p> Lorem Ipsum Dolor </p> <br />
</body>
</html>
";
if(isset($_POST['install'])) {
$AdminUsername = HtmlSpecialChars(strip_tags($_POST['admin_username']));
$AdminPassword = HtmlSpecialChars(strip_tags($_POST['admin_password']));
$dbhost = strip_tags($_POST['dbhost']);
$dbuser = strip_tags($_POST['dbuser']);
$dbpass = strip_tags($_POST['dbpass']);
$sitename = strip_tags($_POST['sitename']);
if(!$con) {
echo "Please configure database information in config.php";
}
$sql = "CREATE TABLE tickets
(
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(32),
Email VARCHAR(40),
Subject VARCHAR(40),
Message TEXT(500),
open VARCHAR(10),
TimeCreated VARCHAR(40),
TimeUpdated VARCHAR(40),
hash VARCHAR(100)
)";
$sql2 = "CREATE TABLE config
(
AdminUsername VARCHAR(32),
AdminPassword VARCHAR(32)
)";
$sql3 = "INSERT INTO config (AdminUsername,AdminPassword) VALUES ('$AdminUsername','$AdminPassword')";
if(empty($AdminUsername)) {
echo "Admin Username should not be empty!";
} elseif (empty($AdminPassword)) {
echo "Admin Password should not be empty!";
} elseif(empty($dbhost)) {
echo "MySQL Database Host should not be empty!";
} elseif(empty($dbuser)) {
echo "MySQL Database User should not be empty!";
/* } elseif(empty($dbname)) {
echo "MySQL Database Name should not be empty!"; */
} elseif(empty($sitename)) {
echo "Please enter a name for your new installation (It can be whatever you want)";
}
else {
$file = fopen('../configuration/config.php','r+');
fwrite($file, '<?php $dbhost = "'. $dbhost.'"; $dbuser = "'. $dbuser.'"; $dbpass = "'. $dbpass.'"; $dbname = "lol"; $sitename = "'. $sitename.'"; $con = new mysqli ($dbhost, $dbuser, $dbpass, $dbname);');
include('../configuration/config.php');
$con->query("CREATE DATABASE lol");
$con->query($sql);
$con->query($sql2);
$con->query("DELETE FROM config");
$con->query($sql3);
fopen('install.lock','w');
header('Location: ../index.php');
}
}
}
} else {
echo "installation is locked";
}
以下一行让我失望:
$con->query("CREATE DATABASE lol");
有谁知道为什么它不起作用?
答案 0 :(得分:2)
因此,如果我正确理解您的代码,您将代码写入配置文件,然后包含该配置文件以访问您的连接对象。这是问题 - 您的连接对象是在假设已经创建了数据库lol的情况下构建的。这不是真的。
您需要做的是声明没有定义数据库的mysqli对象,如下所示:
fwrite($file, '<?php $dbhost = "'. $dbhost.'"; $dbuser = "'. $dbuser.'"; $dbpass = "'. $dbpass.'"; $sitename = "'. $sitename.'"; $con = new mysqli ($dbhost, $dbuser, $dbpass);');
答案 1 :(得分:0)
我觉得J.Robertsons的回答应该有效。您正在尝试在创建之前连接到DB lol。如果由于某种原因在进行更改后仍然无法正常工作,请将以下内容添加到connect语句中,以便您可以看到错误。
or die("Could not connect: " . mysqli_error());
您的代码应如下所示:
fwrite($file, '<?php $dbhost = "'. $dbhost.'"; $dbuser = "'. $dbuser.'"; $dbpass = "'. $dbpass.'"; $dbname = "lol"; $sitename = "'. $sitename.'"; $con = new mysqli ($dbhost, $dbuser, $dbpass) or die("Could not connect: " . mysqli_error());');