0

I'm using a quiz to print 15 questions, each with agree or disagree possible answers. Each answer aims to identify an answerType. There are 5 different dimensions.

I'm aiming to identify with each dimensions having two opposite options. For example, someone's 'mind' is either 'introverted' or 'extroverted' and so on. I have used the same style of quiz before but can't work out where I am going wrong with the code.

<?php
if (isset($_SESSION["currentUser"])) {
    $testID = 2;    
    $dbQuery = $db->prepare("select * FROM questions WHERE testID = '".$testID."'");
    $dbQuery-> execute();

    echo '<form action="personalityType.php" method="post" id="quiz">';

    while ($dbRow = $dbQuery->fetch (PDO::FETCH_ASSOC)) {
        $questionID=$dbRow["questionID"];
        $questionText=$dbRow["questionText"];
        $dbQuery1 = $db->prepare("select * FROM answerPT WHERE questionID = '".$questionID."'");
        $dbQuery1-> execute($dbParams1);
        echo $questionID;
        echo $questionText; 
        echo "<fieldset id=$questionID>";

        while ($dbRow1 = $dbQuery1->fetch (PDO::FETCH_ASSOC)) {
            echo '<br>';
            $answerID=$dbRow1["answerID"];
            $answerText=$dbRow1["answerText"];
            echo "<input type='radio' id=$answerID value=$answerID name=$questionID><label for='answer'>$answerText</label>";

        }   echo "</fieldset>";
    }
    echo "<input type='submit' name='submit' value='Submit Answers'>";
}
echo '</form>';
?>
<?php 

$introverted =0;
$extroverted =0;
$observant =0;
$intuitive =0;
$thinking =0;
$feeling =0;
$judging =0;
$prospecting =0;
$assertive =0;
$turbulent =0;

if (isset ($_POST['submit'])){
    foreach ($_POST as $key=>$value){
        if ($key != "submit"){
            $dbQuery1 = $db->prepare("select * FROM answerPT WHERE answerID = '".$value."'");
            $dbQuery1-> execute();  

            while ($dbRow = $dbQuery1->fetch (PDO::FETCH_ASSOC)){
                $answerTypeID=$dbRow["answerTypeID"];
                $dbQuery2 = $db->prepare("select * FROM answerType WHERE answerTypeID = '".$answerTypeID."'");
                $dbQuery2-> execute();

                while ($dbRow2=$dbQuery2->fetch()){
                    $answerType=$dbRow2["answerType"];

                    if($answerType=='introverted'){
                        $introverted++;
                    }
                    if($answerType=='extroverted'){
                        $extroverted++;
                    }
                    if($answerType=='observant'){
                        $observant++;
                    }
                    if($answerType=='intuitive'){
                        $intuitive++;
                    }
                    if($answerType=='thinking'){
                        $thinking++;
                    }   
                    if($answerType=='feeling'){
                        $feeling++;
                    }
                    if($answerType=='judging'){
                        $judging++;
                    }
                    if($answerType=='prospecting'){
                        $prospecting++;
                    }
                    if($answerType=='assertive'){
                        $assertive++;
                    }
                    if($answerType=='turbulent'){
                        $turbulent++;
                    }                           


                }
            }

            $dbQuery=$db->prepare("INSERT INTO `userResponse` VALUES (:studentID, :questionID, :answerID)");
            $dbParams=array('studentID'=>$_SESSION["currentUser"], 'questionID'=>$key, 'answerID'=>$value);
            $dbQuery->execute($dbParams); 


        }   
    }
}

$introvertedStore = "empty";
$extrovertedStore = "empty";
$observantStore = "empty";
$intuitiveStore = "empty";
$thinkingStore = "empty";
$feelingStore = "empty";
$judgingStore = "empty";
$prospectingStore = "empty";
$assertiveStore = "empty";
$turbulentStore = "empty";      

if ($introverted>$extroverted){
    $total = $introverted - $extroverted;
    if ($total == 1){
        echo "Mildly introverted";
        $introvertedStore = 'Mildly introverted';
    }
    if ($total == 2){
        echo "Moderately introverted";
        $introvertedStore = 'Moderately introverted';
    }
    if ($total == 3){
        echo "Strongly introverted";
        $introvertedStore = 'Strongly introverted';
    }   
}
else {
    $total = $extroverted - $introverted;
    if ($total == 1){
        echo "Mildly extroverted";
        $extrovertedStore = 'Mildly extroverted';
    }
    if ($total == 2){
        echo "Moderately extroverted";
        $extrovertedStore = 'Moderately extroverted';
    }
    if ($total == 3){
        echo "Strongly extroverted";
        $extrovertedStore = 'Strongly extroverted';
    }   
}

if ($introvertedStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, mind) VALUE ('$currentUser', '$introvertedStore')");        
    $dbQuery2->execute(); 

}
if ($extrovertedStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, mind) VALUE ('$currentUser', '$extrovertedStore')");        
    $dbQuery2->execute(); 

}


if ($observant>$intuitive){
    $total = $observant - $intuitive;

    if ($total == 1){
        echo "Mildly observant";
        $observantStore = 'Mildly observant';
    }
    if ($total == 2){
        echo "Moderately observant";
        $observantStore = 'Moderately observant';
    }
    if ($total == 3){
        echo "Strongly observant";
        $observantStore = 'Strongly observant';
    }   
}
else {
    $total = $intuitive - $observant;
    if ($total == 1){
        echo "Mildly intuitive";
        $intuitiveStore = 'Mildly intuitive';
    }
    if ($total == 2){
        echo "Moderately intuitive";
        $intuitiveStore = 'Moderately intuitive';
    }
    if ($total == 3){
        echo "Strongly intuitive";
        $intuitiveStore = 'Strongly intuitive';
    }   
}

if ($observantStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, energy) VALUE ('$currentUser', '$observantStore')");        
    $dbQuery2->execute(); 

}
if ($intuitiveStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, energy) VALUE ('$currentUser', '$intuitiveStore')");        
    $dbQuery2->execute(); 

}


if ($thinking>$feeling){
    $total = $thinking - $feeling;
    if ($total == 1){
        echo "Mildly thinking";
        $thinkingStore = 'Mildly thinking';
    }
    if ($total == 2){
        echo "Moderately thinking";
        $thinkingStore = 'Moderately thinking';
    }
    if ($total == 3){
        echo "Strongly thinking";
        $thinkingStore = 'Strongly thinking';
    }   
}
else {
    $total = $feeling - $thinking;
    if ($total == 1){
        echo "Mildly feeling";
        $feelingStore = 'Mildly feeling';
    }
    if ($total == 2){
        echo "Moderately feeling";
        $feelingStore = 'Moderately feeling';
    }
    if ($total == 3){
        echo "Strongly feeling";
        $feelingStore = 'Strongly feeling';
    }   
}

if ($introvertedStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, nature) VALUE ('$currentUser', '$thinkingStore')");     
    $dbQuery2->execute(); 

}
if ($extrovertedStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, nature) VALUE ('$currentUser', '$feelingStore')");      
    $dbQuery2->execute(); 

}

if ($judging>$prospecting){
    $total = $judging - $prospecting;
    if ($total == 1){
        echo "Mildly judging";
        $judgingStore = 'Mildly judging';
    }
    if ($total == 2){
        echo "Moderately judging";
        $judgingStore = 'Moderately judging';
    }
    if ($total == 3){
        echo "Strongly judging";
        $judgingStore = 'Strongly judging';
    }   
}
else {
    $total = $prospecting - $judging;
    if ($total == 1){
        echo "Mildly prospecting";
        $prospectingStore = 'Mildly prospecting';
    }
    if ($total == 2){
        echo "Moderately prospecting";
        $prospectingStore = 'Moderately prospecting';
    }
    if ($total == 3){
        echo "Strongly prospecting";
        $prospectingStore = 'Strongly prospecting';
    }   
}

if ($judgingStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, tactics) VALUE ('$currentUser', '$judgingStore')");     
    $dbQuery2->execute(); 

}
if ($prospectingStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, tactics) VALUE ('$currentUser', '$prospectingStore')");     
    $dbQuery2->execute(); 

}

if ($assertive>$turbulent){
    $total = $assertive - $turbulent;
    if ($total == 1){
        echo "Mildly assertive";
        $assertiveStore = 'Mildly assertive';
    }
    if ($total == 2){
        echo "Moderately assertive";
        $assertiveStore = 'Moderately assertive';
    }
    if ($total == 3){
        echo "Strongly assertive";
        $assertiveStore = 'Strongly assertive';
    }   
}
else {
    $total = $turbulent - $assertive;
    if ($total == 1){
        echo "Mildly turbulent";
        $turbulentStore = 'Mildly turbulent';
    }
    if ($total == 2){
        echo "Moderately turbulent";
        $turbulentStore = 'Moderately turbulent';
    }
    if ($total == 3){
        echo "Strongly turbulent";
        $turbulentStore = 'Strongly turbulent';
    }   
}

if ($assertiveStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, identity) VALUE ('$currentUser', '$assertiveStore')");      
    $dbQuery2->execute(); 

}
if ($turbulentStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, identity) VALUE ('$currentUser', '$turbulentStore')");      
    $dbQuery2->execute(); 

}


?>
zero323
  • 322,348
  • 103
  • 959
  • 935
  • What have you tried? Where are you stuck? You could benefit in breaking out some of your logic into class functions I would think. Right now this is a garbled block of code that makes it hard to read.. – Woodrow Apr 25 '18 at 19:49
  • From what I can see, and compared to my other (working) similar quiz. It should be inserting the correct personality type into the tables in the DB – Dave Farquhar Apr 25 '18 at 19:50
  • You're not using `prepare()` correctly, and as a result, your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use a placeholder like `:testId` in the query string instead of the quoted variable `$testId`. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Apr 25 '18 at 20:03

0 Answers0