dongyuanliao6204 2015-04-05 22:21
浏览 24
已采纳

DB连接常量在ajax调用上抛出错误

I'm making a test prep app. I store questions and answers in a DB, and when the user is taking the quiz, I instantiate the Question class defined in content.php and call its getQuestions function to loop through and return data for the current question ID.

    $question = new Question($questionID); 
    echo $question->getQuestions();

Which connects to the DB, runs the query for that question ID, and echos out a number of options as radio buttons:

while ($row = $imgpath->fetch_assoc()) {
            $this->id = $row['question_id'];
            echo '
                    <p>' . $row['question']. '</p><br/>
                    <br/><input type="radio" name="choice1" id="c1"> ' . $row['choice_1'] . ' , etc

The issue I'm having is, when the user selects a choice, and clicks the check Answer button, I'm getting breaking DB connection constants...

Use of undefined constant DB_HOST - assumed 'DB_HOST' in C:\wamp\www\...\php\dbconnect.php on line 2

The check answer button:

echo '<br/><br/><button id="checkAnswer" onclick="checkVal()">Check Answer</button>';

Which calls:

                function checkVal() {
                    var chx = document.getElementsByTagName('input');
                    var userAnswer = null;
                    for (var i = 0; i < chx.length; i++) {
                        if (chx[i].type === 'radio' && chx[i].checked) {
                            userAnswer = chx[i].id;
                        }
                    }

                    $.get("./php/content.php", {_input : userAnswer, "_functionToRun" : "getAnswer"},
                        function(returned_data) {
                            $("#output").html(returned_data);
                        }
                    );
                }

Which returns:

$functionToRun = (isset($_GET['_functionToRun']) ? ($_GET['_functionToRun']) : 0); 
if (isset($_GET['_functionToRun']) && !empty($_GET['_functionToRun'])) {
    include 'dbconnect.php';
    if ($functionToRun == "getAnswer") {
        $qry = 
            "SELECT q.*
            FROM questions q
            WHERE question_id = $questionID";

        $result = $mysqli->query($qry); 
        mysqli_close($mysqli);

        $row = $result->fetch_assoc();
        echo $row['answer'];    
    }
}

Question is: Why only on the ajax call are the dbconnection constants coming back undefined? Yet when I first load the page, the constants throw no errors?


dbconnect file: in root/php folder, in same folder as content.php

$mysqli = new mysqli(DB_HOST, DB_UN, DB_PW, DB_NAME);

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (Error code: " . $mysqli->connect_errno . ")... " . $mysqli->connect_error;

}

config.php: file in the root of website

if(!defined("DB_HOST")) define("DB_HOST", "stuff");
if(!defined("DB_UN")) define("DB_UN", "stuff");
if(!defined("DB_PW")) define("DB_PW", "stuff");
if(!defined("DB_NAME")) define("DB_NAME", "stuff");

config.php is loaded from my index.php file which determines which HTML to load:

include './config.php';
$action = isset($_GET['action']) ? $_GET['action'] : "";
switch($action) {   
    case 'practice':
        require(TEMPLATE_PATH . "/practice.php");
        break;
    case 'about':
        require(TEMPLATE_PATH . "/about.php");
        break, etc...
  • 写回答

1条回答 默认 最新

  • dtkwt62022 2015-04-05 23:05
    关注

    Since DB_HOST is defined in config.php, it seems that you do include config.php in the initial page load, but not when you call content.php from AJAX.

    Now, the part that includes config.php is missing from your question, so I can't see exactly what is going on, but likely:

    • config.php is included in an if of which the condition is not met.
    • content.php is in a subfolder or for another reason the path to config.php is invalid.

    To solve this, I would take a couple of steps: Firstly, use require_once instead of include for the config file. This would cause the script to fail with a clear message when the file cannot be found. Also, you can call it multiple times without problems, so every file that needs the config can safely include it with require_once.

    Secondly, consider including the config file in dbconnect.php. After all, that file uses the constants, so it makes sense that it includes the config file they are in.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导