Javascript 用ajax和php验证mysql连接

Javascript 用ajax和php验证mysql连接,javascript,php,mysql,ajax,Javascript,Php,Mysql,Ajax,我正在尝试为连接到数据库的网站创建安装程序,如果不存在,将创建数据库和表 我正在添加一个mysql连接验证表单,以确保服务器实际处于联机状态,这样,如果失败,他们就不必一次又一次地填写表单 它每次都会发回数据,以下是我的结果: 如果主机确实是主机,并且输入了正确的凭据,则会发送一条成功消息 如果主机确实是主机,并且输入了不正确的凭据,则会发送失败消息 如果主机不是主机,并且输入了随机凭据,则不会返回任何消息,并且控制台中存在故障 我想知道这是为什么,有什么想法吗 checkdb.php &l

我正在尝试为连接到数据库的网站创建安装程序,如果不存在,将创建数据库和表

我正在添加一个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));
    }

?>

$(窗口)。加载(函数(){
$('#checkdb')。提交(函数(事件){
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({
键入:“POST”,
url:“/install/checkdb.php”,
数据:formData,
成功:函数(json_数据){
var data_数组=$.parseJSON(json_数据);
if(数据数组['error']==false){
$.notify(数据数组['text'],“success”);
$('#checkdb input').addClass('input-success');
}否则{
$.notify(数据数组['text'],“error”);
$('#checkdb input').addClass('input-failure');
}
},
失败:函数(){
$.notify(“提交AJAX请求时出错。”,“错误”);
}
});
});
});
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>

MCManager安装程序

控制台中的故障是什么?@AdamKonieska意外令牌@AdamKonieska I console.log'd返回的数据,它返回2002 sql错误或“未知主机”。这很好!但是我如何添加它以弹出一条消息呢?不确定,但您可能需要清理进入
$.notify()
的输入。如果
data\u array['text']
包含的
getMessage()
添加的内容,这可能是您的问题。好的,谢谢,我会调查一下。控制台中的故障是什么?@AdamKonieska意外令牌@AdamKonieska I控制台。记录返回的数据,并返回2002 sql错误或“未知主机”. 这很好!但是我如何添加它以弹出一条消息呢?不确定,但您可能需要清理进入
$.notify()
的输入。如果
data\u array['text']
中的
getMessage()
添加的内容,这可能是您的问题。好的,谢谢,我会研究它。
<?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>