我尝试为连接到数据库的网站创建安装程序,如果不存在则会创建数据库和表。
我添加了一个mysql连接验证表单,以确保服务器实际上在线,这样如果失败,他们就不必一次又一次地填写表单。
每次都会发回数据,这些都是我的结果:
我想知道为什么会这样,有什么想法吗?
checkdb.php
<?php
if( empty($_POST) ) {
$output = array("error" => true, "text" => "You didn't fill out the form.");
die(json_encode($output));
} else {
if( empty($_POST["db_host"]) ) {
$output = array("error" => true, "text" => "You didn't fill out the host field.");
echo json_encode($output);
die();
}
if( empty($_POST["db_user"]) ) {
$output = array("error" => true, "text" => "You didn't fill out the username field.");
echo json_encode($output);
die();
}
try {
$db = new PDO("mysql:host=" . $_POST["db_host"], $_POST["db_user"], $_POST["db_pass"]);
if($db == false) {
$output = array("error" => true, "text" => "There was an error connecting to the database.");
die(json_encode($output));
} else {
$output = array("error" => false, "text" => "Successfully connected to database, proceed with installation.");
die(json_encode($output));
}
//$db->exec("CREATE DATABASE `mcm_db`;") or die(print_r($db->errorInfo(), true));
//$sql = file_get_contents("defaults/sql.sql");
} catch(PDOException $ex) {
$output = array("error" => true, "text" => $ex->getMessage());
die(json_encode($output));
}
$output = array("error" => true, "text" => "TEST THIS NEVER RETURNS");
die(json_encode($output));
}
?>
install.php(充当javascript文件)
<?php
if( file_exists("../../app/Configuration/config.php") ) {
require "../../app/Configuration/config.php";
} else if( file_exists("../../install/defaults/config.php") ) {
require "../../install/defaults/config.php";
} else {
die("Could not find the default configuration file or the modified configuration file.");
}
header('Content-Type: application/javascript');
?>
$(window).load(function() {
$('#checkdb').submit(function(event) {
event.preventDefault();
var formData = {
'db_host': $('input[name=db_host]').val(),
'db_user': $('input[name=db_user]').val(),
'db_pass': $('input[name=db_pass]').val()
};
$.ajax({
type: 'POST',
url: '<?php echo URL; ?>/install/checkdb.php',
data: formData,
success: function (json_data) {
var data_array = $.parseJSON(json_data);
if (data_array['error'] == false) {
$.notify(data_array['text'], "success");
$('#checkdb input').addClass('input-success');
} else {
$.notify(data_array['text'], "error");
$('#checkdb input').addClass('input-failure');
}
},
fail: function () {
$.notify("There was an error while submitting the AJAX request.", "error");
}
});
});
});
的index.php
<?php
if( file_exists("../app/Configuration/config.php") ) {
require "../app/Configuration/config.php";
} else if( file_exists("../install/defaults/config.php") ) {
require "../install/defaults/config.php";
} else {
die("Could not find the default configuration file or the modified configuration file.");
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MCManager Installer</title>
<link rel="stylesheet" media="screen" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="<?php echo URL; ?>/resources/css/bootstrap.min.css"></link>
<link rel="stylesheet" href="<?php echo URL; ?>/resources/css/css.install.php" />
<script type="text/javascript" src="<?php echo URL; ?>/resources/js/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo URL; ?>/resources/js/jquery.notify.js"></script>
<script type="text/javascript" src="<?php echo URL; ?>/resources/js/bootstrap.min.js"></script>
</head>
<body>
<div class="content container">
<div id="checkdb_msg" class="msg"><p></p></div>
<div class="module">
<div class="module-header">
<h4>MCManager Installer</h4>
</div>
<div class="module-body module-padding">
<form id="checkdb" method="post" action="<?php echo URL;?>/installer/checkdb.php">
<div class="row">
<div class="col-md-8">
<input type="text" name="db_host" placeholder="Host">
<br />
<input type="text" name="db_user" placeholder="Username">
<br />
<input type="text" name="db_pass" placeholder="Password">
<br />
<input type="submit" name="submit" placeholder="Test Connection" id="checkdb_btn" class="btn btn-primary">
</div>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript" src="<?php echo URL; ?>/resources/js/install.php"></script>
</body>
</html>