melo Posted March 14, 2013 Share Posted March 14, 2013 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>'; } } Quote Link to comment Share on other sites More sharing options...
Barand Posted March 14, 2013 Share Posted March 14, 2013 You missed out getting the returned row from the query result $total_score_g = 'SELECT * FROM tblquestion_mc WHERE QuestionId = "'.$x['QuestionId'].'"'; $total_score_q = mysql_query($total_score_g); $row = mysql_fetch_assoc($total_score_q); if ($row['answer'] == $value) Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 14, 2013 Share Posted March 14, 2013 (edited) 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 March 14, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
melo Posted March 14, 2013 Author Share Posted March 14, 2013 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; Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 14, 2013 Share Posted March 14, 2013 And for the actual quizzes that are taken? Quote Link to comment Share on other sites More sharing options...
melo Posted March 14, 2013 Author Share Posted March 14, 2013 <?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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 14, 2013 Share Posted March 14, 2013 No, how do you store the actual quizzes. You should have a table for quizzes, questions, answers. Then for quizzes taken with questions answered. Google data normalization. Quote Link to comment Share on other sites More sharing options...
melo Posted March 14, 2013 Author Share Posted March 14, 2013 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.