Jump to content

error in simple code, cant spot mistake!


MFA

Recommended Posts

Hi. I have spent this whole afternoon (4 hours) trying to find the mistake causing my code to stop working. It was working fine before I appended all the if  "if ($module == "neuro") {echo 'selected="selected"';}" in the option html tags. Now i get an error saying  "PHP Warning:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /.../.../mcqlist.php on line 241"

 

 

 

 

<?php
 if (($_SESSION['year2']) || (($_SESSION['y2s1']) && ($_SESSION['y2s2'])))
 {
    echo "<h3>".$_SESSION['year2']."</h3>";
 }
if ($_SESSION['y1s1'])
 {
     echo "<h3>".$_SESSION['y2s1']."</h3>";
 }
if ($_SESSION['y2s2'])
 {
    echo "<h3>".$_SESSION['y2s2']."</h3>";
 }
 


    
   $module =$_GET['mod'];
   $questiontype = $_GET['type'];
   $diff = $_GET['diff'];

?>

 
 
 <form name="customlist" action="mcqlistquery.php" method="post"> <!-- use mcqlistcustom or just make it the same page??-->
 <table border="0" width="90%" align="center">
 <tr><td><b>Select Modules</b></td><td><b>Select Question Format</b></td><td><b>Select Difficulty</b></td></tr>
  <tr><td><select name="topic">
  <?php
 
  if (($_SESSION['year2']) || (($_SESSION['y2s1']) && ($_SESSION['y2s2'])))
  {
      echo '
    <option value="all"'; if (($module == "") || ($module=="all")) {echo 'selected="selected"';} echo '>ALL MODULES</option>
        <option value="" ></option>
  <option value="neuro"'; if ($module == "neuro") {echo 'selected="selected"';} echo '>Neuroscience</option>
  <option value="gi"'; if ($module == "gi") {echo 'selected="selected"';} echo '>Gastrointestinal and Liver</option>
  <option value="epistats"'; if ($module == "epistats") {echo 'selected="selected"';} echo '>Epidemiology and Statistics</option>
  <option value="genetics"'; if ($module == "genetics") {echo 'selected="selected"';} echo '>Clinical Genetics</option>
  <option value="renal"'; if ($module == "renal") {echo 'selected="selected"';} echo '>Renal and Urology</option>
  <option value="endocrine"'; if ($module == "endocrine") {echo 'selected="selected"';} echo '>Endocrine</option>
    <option value=""></option>
  <option value="sem1"'; if ($module == "sem1") {echo 'selected="selected"';} echo '>SEMESTER 1 MODULES</option>
  <option value="sem2"'; if ($module == "sem2") {echo 'selected="selected"';} echo '>SEMESTER 2 MODULES</option>  ';
  }
  if ($_SESSION['y2s1'])
  {
      echo '
        <option value="sem1"'; if (($module == "") || ($module=="sem1")) {echo 'selected="selected"';} echo 'selected="selected">SEMESTER 1 MODULES</option>
         <option value="" ></option>
  <option value="neuro"'; if ($module == "neuro") {echo 'selected="selected"';} echo '>Neuroscience</option>
  <option value="gi"'; if ($module == "gi") {echo 'selected="selected"';} echo '>Gastrointestinal and Liver</option>
    <option value="epistats"'; if ($module == "epistats") {echo 'selected="selected"';} echo '>Epidemiology and Statistics</option>';
  }
  if ($_SESSION['y2s2'])
  {
      echo '
        <option value="sem2"'; if (($module == "") || ($module=="sem2")) {echo 'selected="selected"';} echo '>SEMESTER 2 MODULES</option>  
        <option value=""></option>   
     <option value="genetics"'; if ($module == "genetics") {echo 'selected="selected"';} echo '>Clinical Genetics</option>
  <option value="renal"'; if ($module == "renal") {echo 'selected="selected"';} echo '>Renal and Urology</option>
  <option value="endocrine"'; if ($module == "endocrine") {echo 'selected="selected"';} echo '>Endocrine</option>  ';
  }
 
  ?>

  </select></td><td>
 
  <select name="questionformat">
  <option value="all" <?php if (($questiontype == "") || ($questiontype=="all")) {echo 'selected="selected"';} ?>>ALL FORMATS</option>
          <option value="" ></option>
  <option value="mcq" <?php if ($questiontype == "mcq") {echo 'selected="selected"';} ?>>Multiple Choice (MCQ)</option>
  <option value="emq" <?php if ($questiontype == "emq") {echo 'selected="selected"';} ?>>Extended Matching (EMQ)</option>
  <option value="srq" <?php if ($questiontype == "srq") {echo 'selected="selected"';} ?>>Selected Response (SRQ)</option>
  <option value="mcqemq" <?php if ($questiontype == "mcqemq") {echo 'selected="selected"';} ?>>MCQ and EMQ</option>
  <option value="mcqsrq" <?php if ($questiontype == "mcqsrq") {echo 'selected="selected"';} ?>>MCQ and SRQ</option>
  <option value="emqsrq" <?php if ($questiontype == "emqsrq") {echo 'selected="selected"';} ?>>EMQ and SRQ</option></select>

 </td>
  <td> <select name="difficulty">
  <option value="all" <?php if (($diff == "") || ($diff == "all")) {echo 'selected="selected"';} ?>>ALL DIFFICULTIES</option>
          <option value="" ></option>
  <option value="easy" <?php if ($diff == "easy") {echo 'selected="selected"';} ?>>Easy</option>  
  <option value="easymoderate"<?php if ($diff == "easymoderate") {echo 'selected="selected"';} ?>>Easy and Moderate</option>
  <option value="moderate"<?php if ($diff == "moderate") {echo 'selected="selected"';} ?>>Moderate</option>
  <option value="moderatehard" <?php if ($diff == "moderatehard") {echo 'selected="selected"';} ?>>Moderate and Hard</option>
  <option value="hard" <?php if ($diff == "hard") {echo 'selected="selected"';} ?>>Hard</option>
  <option value="easyhard" <?php if ($diff == "easyhard") {echo 'selected="selected"';} ?>>Easy and Hard</option>
  </select></td>
 
  <td>
  <input type="submit" value="Go!" />
  </td></tr>
  </table>
  </form>
 
   <?php
   
     
     if (($module="") || ($diff="") || ($questiontype=""))
     {
        echo "Filter fields not correctly specified!";
        die();
     }
    
     include 'dbyear2.php';
   
if (($_SESSION['year2']) || ($_SESSION['y2s1'])) //alowed subscriptions
{   
switch ($module) //creating whitelist, used for security, prep_stmt can't be used and is better than using escae_string
{
case "neuro":
$tbl = "Neuro_";
break;

case "gi":
$tbl = "GI_";
break;

case "epi":
$tbl = "EPISTATS_";
break;
}
}

if (($_SESSION['year2']) || ($_SESSION['y2s2'])) //allowed subscriptions
{
switch ($module)
{
case "genetics":
$tbl="Genetics_";
break;

case "renal":
$tbl="Renal_";
break;

case "endocrine":    
$tbl = "Endocrine_";
break;
}
}


switch ($questiontype) //question format. whitelist.
{
case "mcq":
$qtype = "MCQ";
break;

case "emq":
$qtype ="EMQ";
break;

case "srq":
$qtype = "SRQ";
break;    

}


$tablefinal = $tbl.$qtype;


 switch ($diff) //whitelist
 {
    
case "easy":
$correctdiff = "Easy";
$imgsrc = "http://.mysite../easy.png";
$imgalt = "easy";
break;

case "moderate":
$correctdiff = "Moderate";
$imgsrc = "http://..mysite../moderate.png";
$imgalt = "moderate";
break;

case "hard":
$correctdiff = "Hard";
$imgsrc = "http://...mywebsite./hard.png";
$imgalt = "hard";
break;    
        
 }
 
$statement = mysqli_query($condbyear2, "SELECT UQN FROM $tablefinal WHERE difficulty= '$correctdiff'");

$baseURL = "http://..my website../mcq.php?uqn=";

while ($row = mysqli_fetch_array($statement, MYSQLI_NUM))
{
foreach ($row as $integer)
{
    $href = $baseURL.$integer;
echo "<a href= '$href'> <img src='$imgsrc' alt='$imgalt' width='80' height='80'> </a>";    
}
}


?>
 
Edited by MFA
Link to comment
Share on other sites

Hi. I have spent this whole afternoon (4 hours) trying to find the mistake causing my code to stop working. It was working fine before I appended all the if  "if ($module == "neuro") {echo 'selected="selected"';}" in the option html tags. Now i get an error saying  "PHP Warning:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /.../.../mcqlist.php on line 241"

 

Let that be a lesson to you. If you copy/paste a bunch of trivial code such as that you are bound to create those types of errors. You could have solved the same thing very easily by creating conditions to define an array of what the option list should be - then processing that array to create the actual options. What you have now would be a bear to try and maintain. But, that code you added shouldn't have any affect on the mysqli_fetch_array() call. Your problem is that the query is failing. So, you may have some problems in the code that define the variables used in the query. Try changing the function call to this:

 

$statement = mysqli_query($condbyear2, "SELECT UQN FROM $tablefinal WHERE difficulty= '$correctdiff'")
    or die("Query: $query<br>Error: " . mysql_error());
Link to comment
Share on other sites

I would probably rewrite the sections towards the bottom with the switch statements to be more efficient as well. But, this will clean up the logic for creating the select options and will be much, much easier to maintain. Not promising I didn't make a syntax error or two but you should be able to fix them if there are and you should be able to see the benefit of using this type of implementation.

 

<?php
if (($_SESSION['year2']) || (($_SESSION['y2s1']) && ($_SESSION['y2s2'])))
{
    echo "<h3>".$_SESSION['year2']."</h3>";
}
if ($_SESSION['y1s1'])
{
     echo "<h3>".$_SESSION['y2s1']."</h3>";
}
if ($_SESSION['y2s2'])
{
    echo "<h3>".$_SESSION['y2s2']."</h3>";
}

$module = $_GET['mod'];
$questiontype = $_GET['type'];
$diff = $_GET['diff'];

function createSelectOptions($optionsList, $selectedValue)
{
    $optionsHTML = '';
    foreach($optionsList as $value => $label)
    {
        $selected = ($selectedValue==$value) ? ' selected="selected"' : '';
        $optionsHTML .= "<option value='{$label}'{$selected}>{$label}</option>\n";
    }
    return $optionsHTML;
}

//Determine list of topic options
if (($_SESSION['year2']) || (($_SESSION['y2s1']) && ($_SESSION['y2s2'])))
{
    $topicOptionsList = array(
        'all' => 'ALL MODULES',
        'neuro' => 'Neuroscience',
        'gi' => 'Gastrointestinal and Liver',
        'epistats' => 'Epidemiology and Statistics',
        'genetics' => 'Clinical Genetics',
        'renal' => 'Renal and Urology',
        'endocrine' => 'Endocrine',
        'sem1' => 'SEMESTER 1 MODULES',
        'sem2' => 'SEMESTER 2 MODULES'
    );
}
elseif ($_SESSION['y2s1'])
{
    $topicOptionsList = array(
        'sem1' => 'SEMESTER 1 MODULES',
        'neuro' => 'Neuroscience',
        'gi' => 'Gastrointestinal and Liver',
        'epistats' => 'Epidemiology and Statistics'
    );
}
elseif ($_SESSION['y2s2'])
{
    $topicOptionsList = array(
        'sem2' => 'SEMESTER 2 MODULES',
        'genetics' => 'Clinical Genetics',
        'renal' => 'Renal and Urology',
        'endocrine' => 'Endocrine'

    );
}
//Create HTML output for the topic options
$topicOptionsHTML = createSelectOptions($topicOptionsList, $module);

//Create list of question options
$questionOptionsList = array(
    'all' => 'ALL FORMATS',
    'mcq' => 'Multiple Choice (MCQ)',
    'emq' => 'Extended Matching (EMQ)',
    'srq' => 'Selected Response (SRQ)',
    'mcqemq' => 'MCQ and EMQ',
    'mcqsrq' => 'MCQ and SRQ',
    'emqsrq' => 'EMQ and SRQ'

);
//Create HTML output for the question options
$questionOptionsHTML = createSelectOptions($questionOptionsList, $questiontype);

//Create HTML output for the difficulty options
$difficultyOptionsList = array(
    'all' => 'ALL DIFFICULTIES',
    'easy' => 'Easy',
    'easymoderate' => 'Easy and Moderate',
    'moderate' => '>Moderate',
    'moderatehard' => 'Moderate and Hard',
    'hard' => 'Hard',
    'easyhard' => 'Easy and Hard'

);

//Create HTML output for the difficulty options
$difficultyOptionsHTML = createSelectOptions($difficultyOptionsList, $diff);
?>
 
 
<form name="customlist" action="mcqlistquery.php" method="post"> <!-- use mcqlistcustom or just make it the same page??-->
<table border="0" width="90%" align="center">
  <tr><td><b>Select Modules</b></td><td><b>Select Question Format</b></td><td><b>Select Difficulty</b></td></tr>
  <tr>
      <td><select name="topic"><?php echo $topicOptionsHTML; ?></select></td>
      <td><select name="questionformat"><?php echo $questionOptionsHTML; ?></select></td>
      <td><select name="difficulty"><?php echo $difficultyOptionsHTML; ?></select></td>
      <td><input type="submit" value="Go!" /></td>
  </tr>
</table>
</form>
 
<?php
     
if (($module="") || ($diff="") || ($questiontype=""))
{
    echo "Filter fields not correctly specified!";
    die();
}
    
include 'dbyear2.php';
   
if (($_SESSION['year2']) || ($_SESSION['y2s1'])) //alowed subscriptions
{   
    switch ($module) //creating whitelist, used for security, prep_stmt can't be used and is better than using escae_string
    {
        case "neuro":
        $tbl = "Neuro_";
        break;

        case "gi":
        $tbl = "GI_";
        break;

        case "epi":
        $tbl = "EPISTATS_";
        break;
    }
}

if (($_SESSION['year2']) || ($_SESSION['y2s2'])) //allowed subscriptions
{
    switch ($module)
    {
        case "genetics":
        $tbl="Genetics_";
        break;

        case "renal":
        $tbl="Renal_";
        break;

        case "endocrine":    
        $tbl = "Endocrine_";
        break;
    }
}


switch ($questiontype) //question format. whitelist.
{
    case "mcq":
    $qtype = "MCQ";
    break;

    case "emq":
    $qtype ="EMQ";
    break;

    case "srq":
    $qtype = "SRQ";
    break;    
}


$tablefinal = $tbl.$qtype;


switch ($diff) //whitelist
{
    
    case "easy":
    $correctdiff = "Easy";
    $imgsrc = "http://.mysite../easy.png";
    $imgalt = "easy";
    break;

    case "moderate":
    $correctdiff = "Moderate";
    $imgsrc = "http://..mysite../moderate.png";
    $imgalt = "moderate";
    break;

    case "hard":
    $correctdiff = "Hard";
    $imgsrc = "http://...mywebsite./hard.png";
    $imgalt = "hard";
    break;    
        
}
 
$statement = mysqli_query($condbyear2, "SELECT UQN FROM $tablefinal WHERE difficulty= '$correctdiff'");

$baseURL = "http://..my website../mcq.php?uqn=";

while ($row = mysqli_fetch_array($statement, MYSQLI_NUM))
{
    foreach ($row as $integer)
    {
        $href = $baseURL.$integer;
        echo "<a href= '$href'> <img src='$imgsrc' alt='$imgalt' width='80' height='80'> </a>";    
    }
}


?>
Link to comment
Share on other sites

Thank you for helping out and teaching me how to simplify such a code however you must appreciate I am still only a beginnner in this field and require further practice before attaining the level of expertise to write such codes.

 

After modifiing my code to as you suggested, I received the following on my page:

 

"Query:
Error:"

 

Edited by MFA
Link to comment
Share on other sites

So I fixed it by creating 2 separate variables for each $_GET command. One specifically for the <select> tags and the other for the switch statements.

 

However, I still want to know why it wasn't working before, could someone help me figure this out please. If I had only one variable for each $_GET command and placed it  below all the <select> tags, the sql query would work. So, the problem is obviously above that point and to do with the section containing the <select> tags.

Link to comment
Share on other sites

Well, it could be any number of things. I think it is pretty obvious that the query was failing, but why it was failing is impossible to determine without knowing what is being passed to the form. You should add error handling to your query to output the actual error from mysql as well as the query. My guess would be that some of the variables you are using in the query were either not getting defined OR they were getting defined with values that made the query invalid.

 

So, here is the query:

 

$statement = mysqli_query($condbyear2, "SELECT UQN FROM $tablefinal WHERE difficulty= '$correctdiff'");

 

$condbyear2 is not defined in that script - I assume it is defined in the include file. The query itself has two variables.

 

$tablefinal is defined here:

 

$tablefinal = $tbl.$qtype;

 

So, now we have to look at where those two variables are defined. $tbl is defined within this code

 

if (($_SESSION['year2']) || ($_SESSION['y2s1'])) //alowed subscriptions
{   
    switch ($module) //creating whitelist, used for security, prep_stmt can't be used and is better than using escae_string
    {
        case "neuro":
        $tbl = "Neuro_";
        break;
        
        case "gi":
        $tbl = "GI_";
        break;
        
        case "epi":
        $tbl = "EPISTATS_";
        break;
    }
}

if (($_SESSION['year2']) || ($_SESSION['y2s2'])) //allowed subscriptions
{
    switch ($module)
    {
        case "genetics":
        $tbl="Genetics_";
        break;
        
        case "renal":
        $tbl="Renal_";
        break;
        
        case "endocrine":    
        $tbl = "Endocrine_";
        break;
    }
}

 

The problem with that is that there are two if() conditions and then switch()es within those without a default. So, if neither of the two if conditions are true OR if one of the two if conditions are true but none of the case statements are true, then $tbl will not be defined and the query will fail. You should code your pages to handle situation when you do not receive the input you expect. It could be that the logic is wrong or that you are receiving input you didn't expect. The code to define $qtype has the sameproblem in that it will only get defined based upon three specific values in a switch().

 

But, now I see why you are having the problem and it is this:

 

     if (($module="") || ($diff="") || ($questiontype=""))
     {
        echo "Filter fields not correctly specified!";
        die();
     }

 

In that if() condition you are ASSIGNING values to those variables not COMPARING them.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.