Jump to content

Get the score of quiz and Insert into table


melo

Recommended Posts

I am having a hard time in getting the score of the quiz I made. I have this code to compute the score and then insert the result into a table. Sorry I am newbie in php.

 

function quizcheck($x)
{
$answer = $x['answer'];
$score = 0;

$total_score_g = 'SELECT * FROM tblquestion_mc WHERE QuestionId = "'.$x['QuestionId'].'"';
$total_score_q = mysql_query($total_score_g);

if ($total_score_q['answer'] == $value)
{
$score = $score + 1;
$value_score = $score *20;

$inscore = 'INSERT INTO tblresult VALUES (
"",
"'.$x['QuizId'].'",
"'.$score.'",
"'.date("Y-m-d").'"
)';

echo '<script>document.location.href="index.php"</script>';
}
}

 

 

Link to comment
Share on other sites

What's the problem? You need to do some basic debugging. See the link in my signature on debugging SQL.

 

I see several MAJOR issues.

 

1. What is $value?

2. Why are you selecting the quiz ID from the questions? What is your table structure? It looks like you may have some issues with that.

3. Your last query could be written much cleaner.

a. Don't put numbers in quotes in MySQL

b. Don't use PHP to calculate the time. Really that field should have a default value.

$inscore = "INSERT INTO tblresult VALUES (NULL, $x['QuizId'], $score, NOW())";

c. You should list the fields, so ideally:

$inscore = "INSERT INTO tblresult(quizId, score) VALUES ($x['QuizId'], $score)";

 

4. You never RUN the query, which is probably your biggest issue.

5. This looks like you'll insert one row for every question. Big problem.

Edited by Jessica
Link to comment
Share on other sites

Thank you for the reply. Sorry I'm new in PHP. I am now editing some flaws in my codes.

Here is my table 

 

CREATE TABLE IF NOT EXISTS `tblquiz` (
  `QuizId` int(11) NOT NULL AUTO_INCREMENT,
  `QuizTitle` varchar(50) NOT NULL,
  `QuizDescription` varchar(50) NOT NULL,
  `Creator` int(11) NOT NULL,
  `Subject` varchar(50) NOT NULL,
  `ClassName` varchar(30) NOT NULL,
  `DateCreate` date NOT NULL,
  `Status` int(11) NOT NULL,
  `Items` int(11) NOT NULL,
  PRIMARY KEY (`QuizId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;

CREATE TABLE IF NOT EXISTS `tblquestion_mc` (
  `QuestionID` int(11) NOT NULL,
  `choice_a` varchar(50) NOT NULL,
  `choice_b` varchar(50) NOT NULL,
  `choice_c` varchar(50) NOT NULL,
  `choice_d` varchar(50) NOT NULL,
  `answer` varchar(50) NOT NULL,
  PRIMARY KEY (`QuestionID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Link to comment
Share on other sites

<?php 
								  
	$quesq = "SELECT * FROM tblquestion_main WHERE QuizId = '".$quiz_id."'";
	$quesg = mysql_query($quesq);
	if($ques = mysql_fetch_array($quesg))
	{
		
	while($ques = mysql_fetch_assoc($quesg)){
			$strQuery = "SELECT * FROM tblquestion_mc WHERE QuestionId = '{$ques['QuestionId']}'";
			$hQuery = mysql_query($strQuery);
			$arMChoices = mysql_fetch_array($hQuery);
			
		switch($ques['QuestionType'])
		{
		case 1:
			?>
            
        <table border="1" cellpadding="2" cellspacing="2" width="500px" bordercolor="#999999" style="margin-top:20px;" align="center">
        <tr>
		<td width="100px" align="left" style="border:none;"> <b> Multiple Choice <br /> <pre> Direction:  </b> Choose the letter of the correct answer. </td>
		</tr>	 
    
        <tr>	
		<td width="100px" align="left" style="border:none;"> Question : <?php echo $ques['Question']; ?></td>
		</tr>
    	<tr>
		<td width="100px" align="left" style="border:none;"> A.  <?php echo $arMChoices['choice_a']; ?></td>
		</tr>    
   		<tr>
   		<td width="100px" align="left" style="border:none;"> B.  <?php echo $arMChoices['choice_b']; ?></td>
		</tr>
    	<tr>
    	<td width="100px" align="left" style="border:none;"> C.  <?php echo $arMChoices['choice_c']; ?></td>
		</tr>
    	<tr>
    	<td width="100px" align="left" style="border:none;"> D.  <?php echo $arMChoices['choice_d']; ?></td>
		</tr>
                <tr>
            	<td align="left" style="border:none;"> Answer:
             
			<select name="answer">
            <option>--Select Choices--</option>
				<option name ="StudentAnswer<?php echo $ques['QuestionId']?>" value="A">A</option>
				<option name ="StudentAnswer<?php echo $ques['QuestionId']?>" value="B">B</option>
                <option name ="StudentAnswer<?php echo $ques['QuestionId']?>" value="C">C</option>
                <option name ="StudentAnswer<?php echo $ques['QuestionId']?>" value="D">D</option>
			</select>
            </td>
        </tr>
		</table>
    
  
          <?php
		  break;
		  case 2:
			?>
            <table border="1" cellpadding="2" cellspacing="2" width="500px" bordercolor="#999999" style="margin-top:20px;" align="center">
            <tr>
			<td width="200px" align="left" style="border:none;"> <b> Identification  <br /> <pre> Direction:  </b> Write the correct answer. </td>
			</tr>
            
            <tr><td width="100px" align="left" style="border:none;">Question : <?php echo $ques['Question']; ?> </td></tr>
	 		<tr><td width="100px" align="left" style="border:none;">Answer :
			 <input type="text" name="StudentAnswer<?php echo $ques['QuestionId']?>" style="width:210px; margin:0px;"/></td>
    		 </tr>
			 </td></tr>
         	</table>
    
          <?php
		  break;
		  case 3:
			?>
            <table border="1" cellpadding="2" cellspacing="2" width="500px" bordercolor="#999999" style="margin-top:20px;" align="center">
           	<tr>
			<td width="100px" align="left" style="border:none;"> <b> True or False <br /> <pre> Direction:  </b> Write TRUE if the statement is correct otherwise write FALSE. </td>
			</tr>                   
            <tr><td width="100px" align="left" style="border:none;">Question : <?php echo $ques['Question']; ?> </td></tr>
	 		<tr><td align="left" style="border:none;">Answer :
			<select name="answer">
            	<option>--Select Choices--</option>
				<option name ="StudentAnswer<?php echo $ques['QuestionId']?>" value="True" >True</option>
				<option name ="StudentAnswer<?php echo $ques['QuestionId']?>" value="False">False</option></select>
                </td>
    		</tr>
            </table>
            
            
            <?php
	    }
	}
    }
}
?>
  
		<table border="1" cellpadding="2" cellspacing="2" width="500px" bordercolor="#999999" style="margin-top:20px;" align="center">
        <tr>
        <td align="center" style="border:none;" colspan="2"><input type="submit" name="quizcheckSubmit" value="submit" onclick="return confirm('Are you sure you want to end the quiz?')" style="width:150px; height:30px;"/></a></td>
        </tr>
        </table>

 

and this is the codes for actual quiz. I forgot to tell that i have 3 types of questions which are multiple choice, true or false and identification.

Link to comment
Share on other sites

oh sorry. this is my table where I store quizzes

 

CREATE TABLE IF NOT EXISTS `tblquiz` (
  `QuizId` int(11) NOT NULL AUTO_INCREMENT,
  `QuizTitle` varchar(50) NOT NULL,
  `QuizDescription` varchar(50) NOT NULL,
  `Creator` int(11) NOT NULL,
  `Subject` varchar(50) NOT NULL,
  `ClassName` varchar(30) NOT NULL,
  `DateCreate` date NOT NULL,
  `Status` int(11) NOT NULL,
  `Items` int(11) NOT NULL,
  PRIMARY KEY (`QuizId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;

 

the table where I store all the questions

 

CREATE TABLE IF NOT EXISTS `tblquestion_main` (
  `QuestionId` int(11) NOT NULL AUTO_INCREMENT,
  `QuizId` int(11) NOT NULL,
  `Question` varchar(50) NOT NULL,
  `QuestionType` int(11) NOT NULL,
  `DateCreate` date NOT NULL,
  `Creator` varchar(25) NOT NULL,
  PRIMARY KEY (`QuestionId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=47 ;

 

table for multiple choice

CREATE TABLE IF NOT EXISTS `tblquestion_mc` (
  `QuestionID` int(11) NOT NULL,
  `choice_a` varchar(50) NOT NULL,
  `choice_b` varchar(50) NOT NULL,
  `choice_c` varchar(50) NOT NULL,
  `choice_d` varchar(50) NOT NULL,
  `answer` varchar(50) NOT NULL,
  PRIMARY KEY (`QuestionID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

table for true or false and identification

 

CREATE TABLE IF NOT EXISTS `tblquestion_default` (
  `QuestionID` int(11) NOT NULL,
  `answer` varchar(50) NOT NULL,
  PRIMARY KEY (`QuestionID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

this is where the answers of every students will be store

 

CREATE TABLE IF NOT EXISTS `tblquestion_student` (
  `ScoreId` int(11) NOT NULL AUTO_INCREMENT,
  `QuizID` int(11) NOT NULL,
  `QuestionID` int(11) NOT NULL,
  `StudentAnswer` varchar(50) NOT NULL,
  `Status` int(11) NOT NULL,
  PRIMARY KEY (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

and the scores

CREATE TABLE IF NOT EXISTS `tblresult` (
  `ScoreId` int(11) NOT NULL AUTO_INCREMENT,
  `QuizId` int(11) NOT NULL,
  `Score` int(11) NOT NULL,
  `Date` date NOT NULL,
  PRIMARY KEY (`ScoreId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

are those what your looking for? or not? 

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.