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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么