How to set up a MySQL database specifically for a quiz based on different "types"

I am a beginner in the world of HTML, PHP, JavaScript, and MySQL. I have developed a quiz using JavaScript and HTML, but now I believe I need a database to store all the questions and answers. I am currently using PHPMyAdmin to set up the database and tables.

The quiz I have created is a type quiz, similar to a "What type of ____ are you?" quiz (e.g., color, dog, personality) where there is no one correct answer to each question.

I am uncertain about what exactly needs to be included in my database for the quiz to function properly. I can provide you with a basic layout of my code if that would be helpful. I apologize if my explanation is unclear.

JavaScript

<head>
<title>Questions</title>
<script language="Javascript">

function process()
{
var Ans1 = 0;
var Ans3 = 0;
var Ans4 = 0;
var Ans2 = 0;

var f = document.f;
var i = 0;

for (i = 0; i < f.q1.length; i++) if (f.q1[i].checked) value = f.q1[i].value;
    if (value == "1") { Ans3++; } <!--put in order with least likely answer first-->
    if (value == "2") { Ans4++; }
    if (value == "3") { Ans2++; }
    if (value == "4") { Ans1++; }

for (i = 0; i < f.q2.length; i++) if (f.q2[i].checked) value = f.q2[i].value;
    if (value == "1") { Ans3++; } <!--put in order with least likely answer first-->
    if (value == "2") { Ans4++; }
    if (value == "3") { Ans2++; }
    if (value == "4") { Ans1++; }

for (i = 0; i < f.q3.length; i++) if (f.q3[i].checked) value = f.q3[i].value;
    if (value == "1") { Ans3++; } <!--put in order with least likely answer first-->
    if (value == "2") { Ans4++; }
    if (value == "3") { Ans2++; }
    if (value == "4") { Ans1++; }

...

var out = "Ans1";
i = Ans1;

if (Ans4 > i) { out ="Ans4"; i = Ans4; }
if (Ans3 > i) { out ="Ans3"; i = Ans3; }
if (Ans2 > i) { out ="Ans2"; i = Ans2; }
location.href = out + ".shtml";
}

function err(msg, url, line)
{
location.href = "error.html";
}

//window.onerror = err;
// -->
</script>
</head>

HTML Quiz Question/Answer Options

<body>
<?php include ('SiteMenu'); ?>
Answer the questions below...

<form name="f">

<b>What is your answer to question #1?<br></b>
    <input type="radio" name="q1" value="4">Ans1.<br>
    ...

Thank you for taking our quiz! <br>
<input type="button" value="Find my Result!" onclick="process();"><br><br>
</form>
</body>
</html> 

UPDATE: I am trying to understand how to set up the database. This is what I have in mind: I have 2 tables: questions and answers The questions table looks like:

qID      aID      qText
q1                What is your favorite color?
q2                This is the second question

and so on

Then, the answers table is structured as follows:

qID      valID      aText
q1                  Green
q1                  Blue
q1                  Red
q2                  question 2 answer choice1
q2                  question 2 answer choice2

and so on

Answer №1

Essentially, the information you need includes:

Questions, ID, and Text
Choices ID, QuestionID, and the corresponding text

Answers entail:

QuestionID, ChoiceID

Questions Table
Id Text 
1  'What is your favorite animal?'

Choices Table
Id, QuestionID, Text
1   1           'Dog'
2   1           'Cat'
3   1           'Bird'
4   1           'Fish'

Answers
UserID QuestionID ChoiceID
123      1          2

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Variable declared in $scope suddenly losing its definition

Within my directive controller, I've implemented a $watch function as follows: $scope.$watch(function () { return service.abc; }, function(newVal, oldVal) { $scope.abc = {abc: newVal}; }); However, I've encountered issues with the variabl ...

Pausing a running function in React

Exploring Visual Sorting Algorithms In the process of creating a visual sorting algorithms tool for educational purposes, I have developed a function called sortArray() that handles the animation of the sorting process on arrays. The functionality is evid ...

Tips for accessing touch events within the parent component's area in React Native

I implemented the code below in my React Native app to disable touch functionality on a specific child component. However, I encountered an issue where the touch event was not being detected within the area of the child component. How can I fix this prob ...

Double the fun: JavaScript array appears twice!

I am currently working on displaying the selected filters from checkboxes. Initially, I create an array containing the values selected from the checkboxes and then aim to add them to a string. Suppose I have two checkboxes labeled: label1 and label2, my a ...

Encountering "Cannot set headers after they are sent to the client" error when making a post request with Node.js, Express, and MySQL

Whenever I attempt to return MySQL results through an express post request, I consistently encounter the following error: (node:3743) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client ...

Sending the user to their destination

There is a code in place that triggers when an email is sent to the user, containing a link that leads them to a specific endpoint upon opening. I am experiencing issues with redirection at the end of the function. The redirect does not seem to be working ...

Exploring the Power of React's Ref API

Currently, I am following tutorials on Udemy by Max where he discusses how to work with Ref Api's in React 16.3. In one of the lectures, he demonstrated creating a ref inside a container class, not App.js, using this.lastref = React.createRef();. He ...

Is it possible for an AJAX request to return both HTML data and execute callback functions simultaneously?

Is it possible to update the content of an HTML div and call a JavaScript function with specific parameters obtained through AJAX after the completion of the AJAX request, all within a single AJAX call? ...

The replacement of className in inline SVG is not permitted (Error: 'Cannot read property 'className.replace' of undefined')

I've hit a roadblock trying to manipulate classes within an SVG document to modify the rect elements. The code works smoothly on divs in an external document, but I've tried several other solutions as well – all listed below. I'm still rel ...

Creating a webpage that seamlessly scrolls and dynamically loads elements

Currently, I am working on a dynamic website that loads new elements as you scroll down the page. Here is an example of the HTML code: <div>some elements here</div> <div id="page2"></div> And this is the JavaScript code: var dis ...

What causes the high memory consumption of the 'readdirSync' method when handling directories with a large number of files?

Consider the NodeJS code snippet below: var fs = require('fs'); function toMb (byteVal) { return (byteVal / 1048576).toFixed(2); } console.log('Memory usage before "readdirSync" operation: ', toMb(process.memoryUsage()['heap ...

Determining the successful completion of an ajax request using jQuery editable plugin

I recently started using Jeditable to enable inline editing on my website. $(".video_content_right .project_description").editable(BASE_URL+"/update/description", { indicator: "<img src='" + BASE_URL + "/resources/assets/front/images/indicator ...

Troubleshooting: Jquery show effects not functioning as expected

Currently, I am facing an issue where I am attempting to display a fixed div using the show function of jQuery. Although the show function itself is working properly, when I try to include an effect from jQuery UI, it does not seem to work as expected. Bot ...

JSON object fails to iterate with ng-repeat

It must be the scorching temperature... Having a json object that I'm eager to loop through using ng-repeat, it should be straightforward, but alas! it's just not cooperating. Here's the HTML snippet: <a data-ng-repeat="x in template.m ...

Develop a fresh behavior on-the-fly

Here is the HTML code snippet: <div class="bold knowmore login" id="j-6"> <span>...</span> </div> and this jQuery script: $(function(){ $(".login").on("click", function(){ console.log('login clicked!'); $(" ...

How can I update the title of the NavigationBarRouteMapper without altering the current route in React Native?

Here is a snippet of code that outlines the NavigationBarRouteMapper: var NavigationBarRouteMapper = { ... , RightButton(route, navigator, index, navState){ return( <TouchableHighlight onPress={()=>{ //When this ...

Using sinon.js version 1.10, jQuery version 2.1, and making synchronous requests

I have been attempting to simulate a server using sinon.js and calling it with jQuery.ajax, but I am facing issues getting it to work. Here is the code snippet: $(function() { var server = sinon.fakeServer.create(); server.respondWith('POST&apo ...

Inserting HTML content into a DIV with the help of jQuery

Looking for a solution with my index.html file. I want to load other HTML files into a DIV by clicking on buttons. Here's an example of what I'm trying to achieve: I've searched through various examples online, but none seem to work for ...

Creating a process to produce a random number and send it directly to an automated email

I'm currently utilizing a form builder from jqueryform.com to construct a registration form. My goal is to have each registered user assigned with a unique account number, which will be a randomly generated 6-digit number. Additionally, I want the pro ...

Utilize regular expressions in TamperMonkey to extract specific groups of text

I'm currently working on a TamperMonkey userscript that aims to identify URLs matching a specific pattern, visit these pages, extract relevant information, and then update the link for each URL with the extracted text. I'm facing some challenges ...