【问题标题】:PHP code not running successfullyPHP代码未成功运行
【发布时间】:2012-12-29 17:52:24
【问题描述】:

我的问题很简单。我在这里编写的代码绝对不会在网页上产生任何输出。我整天都在做这件事,我确信这是一件非常简单的事情,我错过了一个白痴。所以我很吸引你善良的新鲜眼睛!如果有人能找出这不起作用的原因,我将不胜感激。

前提:

这是一个决策树在线调查,具有以下条件:如果用户已经开始调查,它将在数据库中找到他们,找到他们最后回答的问题并显示下一个。但如果他们还没有开始,它会显示第一个问题。

所有调查问题以及决策树逻辑都保存在数据库中(例如,如果用户为问题 1 选择选项 2,他们将被引导至问题 3,而不是问题 2)。

请假设目前,我是直接从数据库中更新相关信息,而不是在网站上自动更新。

谢谢:)

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:

<body>

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

</body>

【问题讨论】:

  • 无输出表示开启错误报告和显示。你可能有一个致命的错误。 error_reporting(E_ALL); ini_set('display_errors', 1);
  • 我的脚本顶部有这个,但网站上没有错误!
  • 你检查过错误日志吗?
  • 儿子,我想你mayneedsomehelp。如果您清理代码、摆脱那些嵌套循环并学习使用PDO,您的代码逻辑(以及由此产生的错误)可能更容易被发现。
  • 我建议将此代码拆分为函数。一个返回 LastQuestionAnswered 的函数。另一个返回 NextQuestion 的函数基于参数“用户的上一个答案”和“LastQuestionAnswered”...

标签: php html mysql if-statement while-loop


【解决方案1】:

这一点:

if ($StartedQueryCount > 0) {

可能评估为 false,并且没有匹配的 else 标记添加内容。 尝试改变:

    }
?>

与:

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

编辑:

还有,这一点:

} else if (empty($LastQuestionAnswered)) {

可以换成更易读的:

} else {

因为它做的事情完全一样。 在你的 while 循环中,你不断地重新定义 $ToDisplay,我认为这是想要的行为?否则初始化顶部的变量(在 while() 循环之前),如下所示:

$ToDisplay = '';

并将循环内的赋值更改为串联,如下所示:

$ToDisplay = 'text assignment';

收件人:

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

【讨论】:

  • 感谢 Johannes 的建议,但这并没有什么不同。不过,感谢您的意见:)
  • 这些似乎是您遇到的唯一逻辑错误。如果这不能解决问题,那么我建议您按照 Michael Berkowski 的提示并添加 error_reporting(E_ALL);和 ini_set('display_errors', 1);
  • 再次感谢约翰内斯。我已经添加了你的建议,但不幸的是没有更快乐的结局。我打开了错误报告,但它没有显示任何内容。关于重新定义 $ToDisplay,如果此人尚未开始调查,我需要它显示第一个问题,或者如果他们之前已经开始,则显示下一个问题。因此,为什么第一个 $ToDisplay 包含 $SurveyStartedQuestionText,而第二个包含 $SurveyNotStartedQuestionText (注意添加的“Not”。我的脑袋糊涂了!
【解决方案2】:

感谢您的所有帮助!非常感谢大家抽出宝贵的时间。

我终于意识到出了什么问题……

在我的 PHP 代码的第 18 行,我有以下内容:

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

当然应该是这样的:

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

基本上我是从错误的查询中调用行。我因此而感到凝块!

再次感谢大家:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 2020-11-05
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多