简体   繁体   English

PHP代码未成功运行

[英]PHP code not running successfully

My question is very simple. 我的问题很简单。 The code I have written here produces absolutely no output on the webpage. 我在这里编写的代码在网页上绝对没有输出。 I've been at it all day and I'm sure that it's something very simple that I am being an idiot for missing. 我整天都在这里,而且我确信这是一个非常简单的事情,我是一个失踪的白痴。 So I am appealing to your good-natured fresh eyes! 所以我对你温柔的新鲜眼睛很有吸引力! If anyone can spot a reason why this isn't working, I'd be very grateful. 如果有人能够发现为什么这不起作用的原因,我将非常感激。

The premise: 前提:

This is a decision tree online survey that has the following conditions: if a user has already started the survey, it will find them in the database, find their last answered question and display the next one. 这是一个决策树在线调查,具有以下条件:如果用户已经开始调查,它将在数据库中找到它们,找到他们最后回答的问题并显示下一个问题。 But if they haven't started, it will display the first question. 但如果他们还没有开始,它将显示第一个问题。

All survey questions are held in the database as well as the decision tree logic (for instance, if the user chooses option 2 for question 1, they will be directed to question 3, not 2). 所有调查问题都保存在数据库和决策树逻辑中(例如,如果用户为问题1选择选项2,则将针对问题3,而不是2)。

Please assume that for the moment, I am updating relevant info directly from the database and not automating it on the website. 请假设目前,我正在直接从数据库更新相关信息,而不是在网站上自动更新。

Thanks :) 谢谢 :)

PHP: PHP:

    <?php
//Find the latest question reached by the user for display on the page
$sql = mysql_query("SELECT QuestionNumberReached FROM User WHERE EmailAddress = '***'");
$sqlCount = mysql_num_rows($sql);
if ($sqlCount > 0) {
    while ($row = mysql_fetch_array($sql)) {
        $QuestionNumberReached = $row["QuestionNumberReached"];
    }
}
?>

<?php
//Find the last question answered by the user from the database
$StartedQuery = mysql_query("SELECT LastQuestionAnswered FROM User WHERE EmailAddress = '***'");
//Count the number of rows that the query produces
$StartedQueryCount = mysql_num_rows($StartedQuery);
//If data is found, whether it be a number or null, define the value
if ($StartedQueryCount > 0) {
    while ($row = mysql_fetch_array($sql)) {
        $LastQuestionAnswered = $row["LastQuestionAnswered"];
        //If the field has a value and is not null, find the next question from the database
        if (!empty($LastQuestionAnswered)) {
            //Find the User's ID and the ID of the last question answered
            $sqlA = mysql_query("SELECT PKID, LastQuestionAnswered FROM User WHERE EmailAddress = '***'");
            //If the operation produces an error, output an error message
            if (!$sqlA) {
                die('Invalid query for SQLA: ' . mysql_error());
            }
            //Count the number of rows output
            $sqlACount = mysql_num_rows($sqlA);
            //If rows exist, define the values
            if ($sqlACount > 0) {
                while ($row = mysql_fetch_array($sqlA)) {
                    $sqlAPKID = $row["PKID"];
                    $sqlALastQuestionAnswered = $row["LastQuestionAnswered"];
                }
            }
            //Find the answer given by the user to the last answered question
            $sqlB = mysql_query("SELECT Answer FROM Responses WHERE User = $sqlAPKID");
            //If the operation produces an error, output an error message
            if (!$sqlB) {
                die('Invalid query for SQLB: ' . mysql_error());
            }
            //Count the number of rows output
            $sqlBCount = mysql_num_rows($sqlB);
            //If rows exist, define the values
            if ($sqlBCount > 0) {
                while ($row = mysql_fetch_array($sqlB)) {
                    $sqlBAnswer = $row["Answer"];
                }
            }
            //Find the number of the next question to be answered based on the user's previous answer and the question they answered
            $sqlC = mysql_query("SELECT NextQuestion FROM Answers WHERE QuestionNumber = $sqlALastQuestionAnswered AND PKID = $sqlBAnswer");
            //If the operation produces an error, output an error message
            if (!$sqlC) {
                die('Invalid query for SQLC: ' . mysql_error());
            }
            //Count the number of rows output
            $sqlCCount = mysql_num_rows($sqlC);
            //If rows exist, define the values
            if ($sqlCCount > 0) {
                while ($row = mysql_fetch_array($sqlC)) {
                    $sqlCNextQuestion = $row["NextQuestion"];
                }
            }
            //Find the question text pertaining to the ID of the next question that needs to be answered
            $sqlD = mysql_query("SELECT QuestionText FROM Questions WHERE PKID = $sqlCNextQuestion");
            //If the operation produces an error, output an error message
            if (!$sqlD) {
                die('Invalid query for SQLD: ' . mysql_error());
            }
            //Count the number of rows output
            $sqlDCount = mysql_num_rows($sqlD);
            //If rows exist, define the values
            if ($sqlDCount > 0) {
                while ($row = mysql_fetch_array($sqlD)) {
                    $SurveyStartedQuestionText = $row["QuestionText"];
                }
            }
            //Set a string of information that will show the question number and question text as appropriate
            $ToDisplay = '' . $QuestionNumberReached . ': ' . $SurveyStartedQuestionText . '<br /><br />Answer Text Here';
        //If the value for QuestionNumberReached is null, the user has not started the survey
        } else if (empty($LastQuestionAnswered)) {
            //Find the question text of the first question in the survey
            $sql3 = mysql_query("SELECT QuestionText FROM Questions WHERE PKID IN (SELECT FirstQuestion FROM Batch WHERE BatchNumber IN (SELECT BatchNumber FROM User WHERE EmailAddress = '***'))");
            //Count the number of rows output
            $sql3Count = mysql_num_rows($sql3);
            //If rows exist, define the values
            if ($sql3Count > 0) {
                while ($row = mysql_fetch_array($sql3)) {
                    $SurveyNotStartedQuestionText = $row["QuestionText"];
                }
            }
            //Set a string of information that will show the question number and question text as appropriate
            $ToDisplay = '' . $QuestionNumberReached . ': ' . $SurveyNotStartedQuestionText . '<br /><br />Answer Text Here';
        }
    }
}
?>

HTML: HTML:

<body>

<?php
// Display the concatenated information that has been previously defined
echo $ToDisplay;
?>

</body>

This bit: 这一点:

if ($StartedQueryCount > 0) {

probably evaluates to false, and there's no matching else tag that adds content. 可能评估为false,并且没有匹配的else标记添加内容。 Try changing: 尝试改变:

    }
?>

with: 有:

    }
    else {
        $ToDisplay = 'Error: no rows found to display!';
    }
?>

Edit: 编辑:

Also, this bit: 还有,这一点:

} else if (empty($LastQuestionAnswered)) {

Could be replaced with the more readable: 可以用更易读的替换:

} else {

Since it does exactly the same thing. 因为它完全一样。 And within your while loop, you are constantly redefining $ToDisplay, I assume this is wanted behaviour? 在你的while循环中,你不断重新定义$ ToDisplay,我认为这是通缉行为? Otherwise initialize the variable on top (before the while() loop) like so: 否则在顶部初始化变量(在while()循环之前),如下所示:

$ToDisplay = '';

And change the assignments within the loop to concatenations, like so: 并将循环中的赋值更改为连接,如下所示:

$ToDisplay = 'text assignment';

To: 至:

$ToDisplay .= 'text concat'; // look at the dot before =

Thank you for all your help! 谢谢你的帮助! I really appreciate you all taking the time. 我非常感谢你们花时间。

I finally realised what was wrong... 我终于意识到出了什么问题......

On Line 18 of my PHP code, I had the following: 在我的PHP代码的第18行,我有以下内容:

while ($row = mysql_fetch_array($sql)) {

whereas it should of course have been this: 而它当然应该是这样的:

while ($row = mysql_fetch_array($StartedQuery)) {

Essentially I was calling the rows from the wrong query. 基本上我是从错误的查询中调用行。 And I feel a clot because of it! 因为它我感觉凝块!

Thanks again, everyone :) 再次感谢大家:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM