javascript - Cannot retrieve questions/information from phpmyAdmin database tables -
i used youtube guide in order create online quiz, cannot quiz retrieve questions localhost database using mamp.
the following php files using:
quiz.php
<?php /* presentation & test quiz */ session_start(); if(isset($_get['question'])){ $question = preg_replace('/[^0-9]/', "", $_get['question']); $next = $question + 1; $prev = $question - 1; if(!isset($_session['qid_array']) && $question != 1){ $msg = "sorry! no cheating."; header("location: index.php?msg=$msg"); exit(); } if(isset($_session['qid_array']) && in_array($question, $_session['qid_array'])){ $msg = "sorry, cheating not allowed. have start over."; unset($_session['answer_array']); unset($_session['qid_array']); session_destroy(); header("location: index.php?msg=$msg"); exit(); } if(isset($_session['lastquestion']) && $_session['lastquestion'] != $prev){ $msg = "sorry, cheating not allowed. have start over."; unset($_session['answer_array']); unset($_session['qid_array']); session_destroy(); header("location: index.php?msg=$msg"); exit(); } } ?> <!doctype html> <html lang="en"> <head> <title>quiz page</title> <meta charset="utf-8"> <script type="text/javascript"> function countdown(secs,elem) { var element = document.getelementbyid(elem); element.innerhtml = "you have "+secs+" seconds remaining."; if(secs < 1) { var xhr = new xmlhttprequest(); var url = "useranswers.php"; var vars = "radio=0"+"&qid="+<?php echo $question; ?>; xhr.open("post", url, true); xhr.setrequestheader("content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readystate == 4 && xhr.status == 200) { alert("you did not answer question in allotted time. marked incorrect."); cleartimeout(timer); } } xhr.send(vars); document.getelementbyid('counter_status').innerhtml = ""; document.getelementbyid('btnspan').innerhtml = '<h2>times up!</h2>'; document.getelementbyid('btnspan').innerhtml += '<a href="quiz.php?question=<?php echo $next; ?>">click here now</a>'; } secs--; var timer = settimeout('countdown('+secs+',"'+elem+'")',1000); } </script> <script> function getquestion(){ var hr = new xmlhttprequest(); hr.onreadystatechange = function(){ if (hr.readystate==4 && hr.status==200){ var response = hr.responsetext.explode("|"); if(response[0] == "finished"){ document.getelementbyid('status').innerhtml = response[1]; } var nums = hr.responsetext.explode(","); document.getelementbyid('question').innerhtml = nums[0]; document.getelementbyid('answers').innerhtml = nums[1]; document.getelementbyid('answers').innerhtml += nums[2]; } } hr.open("get", "questions.php?question=" + <?php echo $question; ?>, true); hr.send(); } function x() { var rads = document.getelementsbyname("rads"); ( var = 0; < rads.length; i++ ) { if ( rads[i].checked ){ var val = rads[i].value; return val; } } } function post_answer(){ var p = new xmlhttprequest(); var id = document.getelementbyid('qid').value; var url = "useranswers.php"; var vars = "qid="+id+"&radio="+x(); p.open("post", url, true); p.setrequestheader("content-type", "application/x-www-form-urlencoded"); p.onreadystatechange = function() { if(p.readystate == 4 && p.status == 200) { document.getelementbyid("status").innerhtml = ''; alert("thanks, answer submitted"+ p.responsetext); var url = 'quiz.php?question=<?php echo $next; ?>'; window.location = url; } } p.send(vars); document.getelementbyid("status").innerhtml = "processing..."; } </script> <script> window.oncontextmenu = function(){ return false; } </script> <style type="text/css"> .header { text align: center; background-image: url(header_graphic.png); background-repeat: no-repeat; padding-bottom: 20px; } #header-logo { text-align: right; padding-top: 20px; padding-right: 20px; } h2 { text-align: center; font-size: 26px; font-family: weblysleek ui light; color: #ffffff; margin-top: -44px; } .content { padding-top:50px; padding-left: 20px; padding-right: 20px; padding-bottom:50px; font-family: weblysleek ui; font-size: 16px; color: #4a4745; } #status { font-size: 14px; font-family: weblysleek ui; color: #4a4745; } #counter_status { font-size: 14px; font-family: weblysleek ui; color: #4a4745; } #question { font-size: 14px; font-family: weblysleek ui; color: #4a4745; } #answers { font-size: 14px; font-family: weblysleek ui; color: #4a4745; } .footer { width: 100%; margin-top: -10px; } </style> </head> <body onload="getquestion()"> <div class="header"> <div id="header-logo"> <img src="esgplc logo_white_for quiz.png" /> </div> <h2>esgplc health, safety & environment quiz test</h2> </div> <div class="content"> <div id="status"></div> <div id="counter_status"></div> <div id="question"></div> <div id="answers"></div> </div> <script type="text/javascript">countdown(60,"counter_status");</script> <div class="footer"> <img src="http://localhost/quiz/esgplc_bsbs_footer.png" /> </div> </body> </html>
and this, question.php:
<?php /* presentation & test quiz */ session_start(); require_once("scripts/connect_db.php"); $arrcount = ""; if(isset($_get['question'])){ $question = preg_replace('/[^0-9]/', "", $_get['question']); $output = ""; $answers = ""; $q = ""; $sql = mysqli_query("select id questions"); $numquestions = mysqli_num_rows($sql); if(!isset($_session['answer_array']) || $_session['answer_array'] < 1){ $currquestion = "1"; }else{ $arrcount = count($_session['answer_array']); } if($arrcount > $numquestions){ unset($_session['answer_array']); header("location: index.php"); exit(); } if($arrcount >= $numquestions){ echo 'finished|<p>there no more questions. please enter first , last name , click next</p> <form action="useranswers.php" method="post"> <input type="hidden" name="complete" value="true"> <input type="text" name="username"> <input type="submit" value="finish"> </form>'; exit(); } $singlesql = mysqli_query("select * questions id='$question' limit 1"); while($row = mysqli_fetch_array($singlesql)){ $id = $row['id']; $thisquestion = $row['question']; $type = $row['type']; $question_id = $row['question_id']; $q = '<h2>'.$thisquestion.'</h2>'; $sql2 = mysqli_query("select * answers question_id='$question' order rand()"); while($row2 = mysqli_fetch_array($sql2)){ $answer = $row2['answer']; $correct = $row2['correct']; $answers .= '<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label> <input type="hidden" id="qid" value="'.$id.'" name="qid"><br /><br /> '; } $output = ''.$q.','.$answers.',<span id="btnspan"><button onclick="post_answer()">submit</button></span>'; echo $output; } } ?>
connect-db.php
<?php $db_host = 'localhost'; $db_username = 'root'; $db_pass = 'root'; $db_name = 'esgplc_hse_induction'; $con = mysqli_connect($db_host, $db_username, $db_pass, $db_name); if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } ?>
i cant see going wrong. understand mysql changed mysqli have updated in code.
many thanks,
as mentioned in comments. mysqli_query()
function requires database parameter passed it.
- none of queries contain it.
example:
$singlesql = mysqli_query($con, "your query...");
and apply logic queries.
reference:
check errors also:
look using prepared statement also:
Comments
Post a Comment