Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg/projects/

bacon-egg-pizza.php
cquiz.php
create-account.php
create-account111.php
create-quiz.php
create-quiz2.php
createaccount.php
delete-quiz.php
drop-rps-tables.php
legacy-code-rps.php
legacy-index.php
login.php
logincheck.php
newpassword-verify.php
newpassword.php
potential-code.txt
quiz-db-init.php
quizhub.php
quizhub2.php
results.php
rpsaccount-ajax-user-search.php
rpsaccount.php
rpsbetting-ajax-match-finder.php
rpsbetting.php
rpsbettingleaderboard.php
rpschangepassword.php
rpschangepassword.txt
rpsgame-ajax.php
rpsgame.php
rpsgameresults.php
rpshostnewgame.php
rpshub.php
rpsleaderboard.php
rpslib.php
rpslogin.php
rpsmaininclude.php
rpsproject/
rpsusersearch.php
table-init.php
verification.php

quizhub2.php

150 lines UTF-8 Windows (CRLF)
<?php require('logincheck.php'); ?>

<html>
    <h1>Quizhub2</h1>
    <h3>välkommen <?php echo $_SESSION['user']; ?></h3>
    välj quiz
    <br>
        <?php
            $sql 
"SELECT * FROM qlist";
            
$stmt $dbconn->prepare($sql);
            
$stmt->execute();
            while(
$res $stmt->fetch(PDO::FETCH_ASSOC)){
                
$quizName $res['qlname'];
                echo(
'<form action="" method="GET"><button type="submit" name="quiz" value="'.$quizName.'">'.$quizName.'</button>');
            }
        
?>
    </form>

    <?php
        
if(isset($_GET['quiz'])){
            
            
$selectedQuiz $_GET['quiz'];
            
$sql "SELECT * FROM qlist WHERE qlname='$selectedQuiz'";
            
$stmt $dbconn->prepare($sql);
            
$stmt->execute();
            while(
$res $stmt->fetch(PDO::FETCH_ASSOC)){
                
$quizID $res['listid'];
            }

            echo 
$selectedQuiz.'<br><br><form action="" method="POST">';

            
$sql "SELECT * FROM qquestions WHERE qlkey='$quizID'";
            
$stmt $dbconn->prepare($sql);
            
$stmt->execute();
            
$currentQuestionsID=[];
            
$currentQuestions=[];
            while(
$res $stmt->fetch(PDO::FETCH_ASSOC)){
                
$questionID $res['questionid'];
                
array_push($currentQuestionsID$questionID);
                
$questionName htmlentities($res['qqname']);
                
array_push($currentQuestions$questionName);
            }

            
$correctAnswers=[];
            GLOBAL 
$correctAnswers;

            
$qqCounter=0;

            foreach(
$currentQuestionsID as $qID){
                
//$qID = $questionID;
                
$cqname $currentQuestions[$qqCounter];
                
$qqCounter+=1;
                echo 
$cqname;

                
$sql "SELECT * FROM questionanswers WHERE qqkey='$qID'";
                
$stmt $dbconn->prepare($sql);
                
$stmt->execute();
                
$currentAnswers=[];
                
$currentAnswersID=[];
                while(
$res $stmt->fetch(PDO::FETCH_ASSOC)){
                    
$ans htmlentities($res['ans']);
                    if(
$res['correct']==1){
                        
$cans htmlentities($res['ans']);
                        
array_push($correctAnswers$cans);
                    }
                    
                    
$aID $res['akey'];
                    
array_push($currentAnswers$ans);
                    
array_push($currentAnswersID$aID);
                }
                foreach(
$currentAnswers as $curans){
                    echo(
'<input type="radio" name="'.$cqname.'" value="'.$curans.'">'.$curans.'</input>');
                }
                GLOBAL 
$cqname;
                echo 
"<br>";
            }

            echo 
'<input type="submit"></form>';

            if(isset(
$_POST[$cqname])){
                echo 
$_POST[$cqname];
                echo 
'sent';
                
$arrayCounter 0;
                
$totalscore=0;

                
$resultString 'Svar<br>';

                foreach(
$currentQuestions as $question){
                    if(isset(
$_POST[$question])){
                        
$score 0;
                        if(
$_POST[$question] == $correctAnswers[$arrayCounter]){
                            
$score=1;
                            
$totalscore+=1;
                        }
                        
$ca $_POST[$question];

                        if(
$ca == $correctAnswers[$arrayCounter]){
                            
$resultString.= $question.': ditt svar: <span style="color:green">'.$ca.'</span><br>';
                        }
                        else{
                            
$resultString.= $question.': ditt svar: <span style="color:green">'.$ca.' </span>rätt svar: <span style="color:red">'.$correctAnswers[$arrayCounter].'</span><br>';
                        }
                        
                        
/*
                        $cqID = $currentQuestionsID[$arrayCounter];

                        $sql = "SELECT EXISTS(SELECT result FROM qresults WHERE qkey='$cqID' AND ukey='$uID')";
                        $stmt = $dbconn -> prepare($sql);
                        $stmt -> execute();
                        while($res = $stmt->fetch(PDO::FETCH_ASSOC)){
                            $sql = "DELETE FROM qresults WHERE qkey='$cqID' AND ukey='$uID'";
                            $stmt = $dbconn -> prepare($sql);
                            $stmt -> execute();
                        }

                    $sql = "INSERT INTO qresults (qkey,ukey,result) VALUES (?,?,?)";
                    $stmt = $dbconn -> prepare($sql);
                    $data = [$cqID,$uID,$score];
                    $stmt -> execute($data);
                    */

                    
$arrayCounter+=1;
                                    
                    }
                }
                echo 
'ditt resultat: '.$totalscore.'<br>';
                echo 
$resultString;

                
$uID $_SESSION['userid'];

                
$sql "SELECT EXISTS(SELECT * FROM quserresults WHERE quizkey='$quizID' AND userkey='$uID')";
                
$stmt $dbconn -> prepare($sql);
                
$stmt -> execute();
                while(
$res $stmt->fetch(PDO::FETCH_ASSOC)){
                    
$sql "DELETE FROM quserresults WHERE quizkey='$quizID' AND userkey='$uID'";
                    
$stmt $dbconn -> prepare($sql);
                    
$stmt -> execute();
                }

                
$sql "INSERT INTO quserresults (quizkey,userkey,htmldata,totscore) VALUES ($quizID,$uID,?,?)";
                
$stmt $dbconn -> prepare($sql);
                
$data = [$resultString,$totalscore];
                
$stmt -> execute($data);

            }
        }

    
?>

</html>