PHP code not working successfully

My question is very simple. The code I wrote here gives absolutely no result on the web page. I have been in this all day, and I am sure that it is something very simple, that I am an idiot. Therefore, I turn to your good-natured fresh eyes! If anyone can determine the reason why this does not work, I would be very grateful.

Premise:

This is an online survey of the decision tree, which has the following conditions: if the user has already started the survey, he will find them in the database, find his last answer and display the next one. But if they have not started yet, it will display the first question.

All survey questions are stored in the database, as well as the logic of the decision tree (for example, if the user selects option 2 for question 1, they will be directed to question 3, not 2).

Suppose that at the moment I am updating the relevant information directly from the database, rather than automating it on the website.

Thanks:)

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 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 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> 
+4
source share
2 answers

Thank you for your help! I really appreciate you all wasting your time.

I finally realized what was wrong ...

On line 18 of my PHP code, I had the following:

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

whereas this, of course, was as follows:

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

Essentially, I was calling strings from the wrong request. And I feel a clot because of this!

Thanks again:)

0
source

This bit:

 if ($StartedQueryCount > 0) { 

probably evaluated as false, and there is no other else tag that adds content. Try changing:

  } ?> 

with:

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

Edit:

In addition, this bit:

 } else if (empty($LastQuestionAnswered)) { 

May be replaced by a more readable one:

 } else { 

Since he does the same. And in your while loop you are constantly redefining $ ToDisplay, I assume this is a desire for behavior? Otherwise, initialize the variable from above (before the while () loop):

 $ToDisplay = ''; 

And change the assignments in the loop to concatenation, for example:

 $ToDisplay = 'text assignment'; 

To:

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

All Articles