I am currently working on an online Survey, and having issues with a Buffer Error.
In my HTML section, I send the QuestionID - from a recordset which I retrieved earlier in my PHP section - to a function which retrieves the Question-Stem and generates the Answer Options based on the Question-Type (e.g. Yes/No, True/False, Multiple-Choice, etc.).
Here is that code…
<form id="rateThisArticle" action="" method="post">
<fieldset>
<legend>Rate This Article</legend>
<ul>
<?php
// Set Question Number.
$questionNo = 1;
// **************************
// Generate Survey Items. *
// **************************
while (mysqli_stmt_fetch($stmt3)){
generateSurveyItem($dbc, $questionNo, $articleSurveyQuestionID);
// Increment Question Number.
$questionNo = $questionNo + 1;
}//End of GENERATE SURVEY ITEMS
?>
<!-- Submit Form -->
<li id="buttons">
<input type='submit' name='submit' class='button' value='Rate This Article' />
<input type='reset' class='button' value='Cancel' />
</li>
</ul>
</fieldset>
</form>
The problem is that in the Function - which is in a separate script - I try to display an Error-Message if the Query which retrieves the Question and Answers fails.
Here is a snippet…
//****************************************************************************
function generateSurveyItem($dbc, $questionNo, $articleSurveyQuestionID){
// Initialize Variables.
$sessMemberID = (isset($_SESSION['sessMemberID']) ? $_SESSION['sessMemberID'] : '');
// ********************************
// Attempt to Retrieve Item Info. *
// ********************************
// Build query.
$q1 = "SELECT stem, question_type, label_lower, label_upper
FROM article_survey_question
WHERE id=?";
// Prepare statement.
$stmt1 = mysqli_prepare($dbc, $q1);
// Bind variable to query.
mysqli_stmt_bind_param($stmt1, 'i', $articleSurveyQuestionID);
// Execute query.
mysqli_stmt_execute($stmt1);
// Store results.
mysqli_stmt_store_result($stmt1);
// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt1)==2){ //TRY TO CREATE ERROR (should be ==1)
// Item Found.
// Bind result-set to variables.
mysqli_stmt_bind_result($stmt1, $questionStem, $questionType, $labelLower, $labelUpper);
// Fetch record.
mysqli_stmt_fetch($stmt1);
}else{
// Item Not Found.
$_SESSION['resultsCode'] = 'RATE_ARTICLE_QUESTION_NOT_FOUND_2385';
// $resultsCode = 'RATE_ARTICLE_QUESTION_NOT_FOUND_2385';
// Set Error Source.
$errorPage = $_SERVER['SCRIPT_NAME'];
// Redirect to Display Outcome.
header("Location: " . BASE_URL . "/account/results.php");
// Log Function Error.
// logError($dbc, $resultsCode, $errorPage, $sessMemberID);
// End script.
exit();
}//End of ATTEMPT TO RETRIEVE ITEM INFO
// Rest of Function below...
As you can see from the commented-out lines above in my Function, I was originally just logging the error, but then I realized that my website cannot return Questions and Answers then I need to not only log things, but display an Error-Message like “Sorry, a System Error occurred and the Survey cannot be displayed.”
I typically do a good job of putting the logic up in my PHP section, but I am not seeing how to do it here, since I need to call the Function as I loop through the QuestionID’s I found earlier, and that is breaking my Error-Message if my Function fails?!
Can someone help me figure this out??
Thanks,
Debbie