Multipochoice Quiz (20 Random Questions)

Alles, was PHP betrifft, kann hier besprochen werden.

Multipochoice Quiz (20 Random Questions)

Postby hemmo001 » 23. November 2014 19:29

Hi people I need some advice off you please. I'm creating a quiz for the website that I'm creating for my portfolio to help me to get a graduate job.

I want to get 20 questions (at random) I think I have got this sorted now. But when I try and run the quiz the following error shows


Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'user'@'localhost' (using password: YES) in C:\xampp\htdocs\quiz.php on line 3

Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\quiz.php on line 6

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\quiz.php on line 8

This is the code for reference:

Code: Select all
<?php
//Connect with mysql
$db = new mysqli("127.0.0.1", "user", "pass", "db");

//Perform the query to choose random questions
$query = $db->query("SELECT * FROM `table` ORDER BY RAND() LIMIT 20");

while ($row = $query->fetch_assoc()):
    $question = $row['question'];
    echo $question . "<br />";
endwhile;

//close result
$query->close();
//Close connection
$db->close();

session_start();
if (isset($_GET['question'])) {
    $question = preg_replace('/[^0-9]/', "", $_GET['question']);
    $next     = $question + 1;
    $prev     = $question - 1;
} // <--- I added this brace. You need to double-check it logically belongs here
if (!isset($_SESSION['qid_array']) && $question != 1) {
    $msg = "Sorry! No cheating.";
    header("location: start.php?msg=$msg");
    exit();
}
if (isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])) {
    $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
    unset($_SESSION['answer_array']);
    unset($_SESSION['qid_array']);
    session_destroy();
    header("location: start.php?msg=$msg");
    exit();
}
if (isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev) {
    $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
    unset($_SESSION['answer_array']);
    unset($_SESSION['qid_array']);
    session_destroy();
    header("location: start.php?msg=$msg");
    exit();
}
?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Quiz Page</title>

    <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 the question in the allotted time. It will be marked as 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);
    }
 
    function getQuestion(){
    var hr = new XMLHttpRequest();
    hr.onreadystatechange = function(){
    if (hr.readyState==4 && hr.status==200){
    var response = hr.responseText.split("|");
    if(response[0] == "finished"){
    document.getElementById('status').innerHTML = response[1];
    }
    var nums = hr.responseText.split(",");
    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");
    for ( var i = 0; i < 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, Your answer was submitted"+ p.responseText);
    var url = 'quiz.php?question=<?php echo $next;
   
   ?>';
   
    window.location = url;
    }
    }
    p.send(vars);
    document.getElementById("status").innerHTML = "processing...";
    }
    window.oncontextmenu = function(){
    return false;
    }
    </script>


    </head>
     
    <body onLoad="getQuestion()">
    <div id="status">
    <div id="counter_status"></div>
    <div id="question"></div>
    <div id="answers"></div>
    </div>
     
     
    </script>
    </body>
    </html>


Any help would be really appreciated

Rich
hemmo001
 
Posts: 7
Joined: 23. November 2014 18:33
Operating System: windows 7

Re: Multipochoice Quiz (20 Random Questions)

Postby Altrea » 23. November 2014 19:57

Hi rich,

your user "user" with this password "pass" does not have priviledges to access the database from host "127.0.0.1"

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8293
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64

Re: Multipochoice Quiz (20 Random Questions)

Postby hemmo001 » 23. November 2014 20:17

Ahh right ok sorry to sound like an idiot then. How can I let the user have privileges then? Because at one point it was working before I wanted to have 20 questions showing for the quiz at random...

I'm accessing the same database (but a different table) I registered a new user and it was working perfectly. I guess I'm just been stupid and missing something then I take it?

Thanks for any help you can give me

Rich
hemmo001
 
Posts: 7
Joined: 23. November 2014 18:33
Operating System: windows 7

Re: Multipochoice Quiz (20 Random Questions)

Postby hemmo001 » 15. December 2014 18:27

Altrea wrote:Hi rich,

your user "user" with this password "pass" does not have priviledges to access the database from host "127.0.0.1"

best wishes,
Altrea


hey sorry to be such a pain but could you please give me a pointer as to how i sort this out please?
hemmo001
 
Posts: 7
Joined: 23. November 2014 18:33
Operating System: windows 7

Re: Multipochoice Quiz (20 Random Questions)

Postby Nobbie » 15. December 2014 20:04

Start and run "phpmyadmin", you will find a link in the Xampp Menu on the left panel. Inside of phpMyAdmin navigate to "Users".

BUT: if you do not remember that you created a specific user, you will not remember the password. Xampp comes only with one user "root" and the default password is empty. Everything else is ON YOU! We cannot help you on your UserIds. Or can you tell me my name?
Nobbie
 
Posts: 8772
Joined: 09. March 2008 13:04

Re: Multipochoice Quiz (20 Random Questions)

Postby hemmo001 » 15. December 2014 20:16

Nobbie wrote:Start and run "phpmyadmin", you will find a link in the Xampp Menu on the left panel. Inside of phpMyAdmin navigate to "Users".

BUT: if you do not remember that you created a specific user, you will not remember the password. Xampp comes only with one user "root" and the default password is empty. Everything else is ON YOU! We cannot help you on your UserIds. Or can you tell me my name?


ahh right ok i can see where your coming from now ok. I will have a look and see.

I don't understand because its been working fine and this past few weeks it just wont let me run the quiz at all without the error message coming up
hemmo001
 
Posts: 7
Joined: 23. November 2014 18:33
Operating System: windows 7

Re: Multipochoice Quiz (20 Random Questions)

Postby hemmo001 » 15. December 2014 20:26

I have just looked at my xamp control panel this hasnt got anything to do with it by any chance why i'm having trouble?

You are not running with administrator rights! This will work for
09:57:36 [main] most application stuff but whenever you do something with services
09:57:36 [main] there will be a security dialogue or things will break! So think
09:57:36 [main] about running this application with administrator rights!
hemmo001
 
Posts: 7
Joined: 23. November 2014 18:33
Operating System: windows 7

Re: Multipochoice Quiz (20 Random Questions)

Postby hemmo001 » 15. December 2014 20:37

HI guys ok problem is now sorted, it was staring me in the face..... i must wake up at 1am every morning and stay awake for more than 15 hours and then code lol

when i now run the quiz i'm getting this


Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\quiz.php on line 8

this is my code some research i think its telling me something to do with the sessions i have in the code?
Code: Select all
<?php
//Connect with mysql
$db = new mysqli("127.0.0.1", "root", "", "quiz");

//Perform the query to choose random questions
$query = $db->query("SELECT * FROM `table` ORDER BY RAND() LIMIT 20");

while ($row = $query->fetch_assoc()):
    $question = $row['question'];
    echo $question . "<br />";
endwhile;

//close result
$query->close();
//Close connection
$db->close();

session_start();
if (isset($_GET['question'])) {
    $question = preg_replace('/[^0-9]/', "", $_GET['question']);
    $next     = $question + 1;
    $prev     = $question - 1;
} // <--- I added this brace. You need to double-check it logically belongs here
if (!isset($_SESSION['qid_array']) && $question != 1) {
    $msg = "Sorry! No cheating.";
    header("location: start.php?msg=$msg");
    exit();
}
if (isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])) {
    $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
    unset($_SESSION['answer_array']);
    unset($_SESSION['qid_array']);
    session_destroy();
    header("location: start.php?msg=$msg");
    exit();
}
if (isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev) {
    $msg = "Sorry, Cheating is not allowed. You will now have to start over. Haha.";
    unset($_SESSION['answer_array']);
    unset($_SESSION['qid_array']);
    session_destroy();
    header("location: start.php?msg=$msg");
    exit();
}
?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Quiz Page</title>

    <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 the question in the allotted time. It will be marked as 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);
    }
 
    function getQuestion(){
    var hr = new XMLHttpRequest();
    hr.onreadystatechange = function(){
    if (hr.readyState==4 && hr.status==200){
    var response = hr.responseText.split("|");
    if(response[0] == "finished"){
    document.getElementById('status').innerHTML = response[1];
    }
    var nums = hr.responseText.split(",");
    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");
    for ( var i = 0; i < 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, Your answer was submitted"+ p.responseText);
    var url = 'quiz.php?question=<?php echo $next;
   
   ?>';
   
    window.location = url;
    }
    }
    p.send(vars);
    document.getElementById("status").innerHTML = "processing...";
    }
    window.oncontextmenu = function(){
    return false;
    }
    </script>


    </head>
     
    <body onLoad="getQuestion()">
    <div id="status">
    <div id="counter_status"></div>
    <div id="question"></div>
    <div id="answers"></div>
    </div>
     
     
    </script>
    </body>
    </html>


thanks in advance for any info and help

rich
hemmo001
 
Posts: 7
Joined: 23. November 2014 18:33
Operating System: windows 7

Re: Multipochoice Quiz (20 Random Questions)

Postby Altrea » 15. December 2014 23:19

No. Your query() fails.
You should start to use mysqli error methods to show What went wrong
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8293
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64


Return to PHP

Who is online

Users browsing this forum: No registered users and 3 guests