RunCodes
Programming & Tech

How to Create Quiz Website using PHP and MySQL?[With Source Code]

10 8,780

Due to some security issues i have updated this article 🙂

updated !

if you guyz are thinking about how can you create simple quiz website or web based quiz application then you are in the right tutorials.

Before getting into the topic, let’s understand what is the basic concept, how this is programmatically possible and so on. In general quiz application, user select their answer and click send button to submit their answer to the server database (in case of online) or to local database(in case of offline) and then they check their answer by click other button. So, we need to understand programmatically what happen when the user select their answer and click on submit button and what is the operation behind when user click on the check button. Being the programmer you can imagine what operation need to perform to do this using PHP.

So, our basic concept is when the user select their answer, their answer need to be inserted into the database and return some value when user click the another button for checking answer. So, for this we are going to create one database in our MySQL databse it means you need to instant any server application like WAMP Server or XAMPP Server but i am using WAMP Server. And inside that database we are going to create three table. you can give them any name but for our convenient we give the name of these three table as submittedanswer, answer and compare.

And inside each table we are going to create two column i.e. ID is common in all there table and second column name is: qsn in qsn table, ans in answer table and value in compare table. We should store correct answer in answer table by ourself.  So when the user click their answer and click submit button, their answer is store in submittedanswer table and at the same time that submitted answer is compare with the previously stored correct answer which is store in answer table and should store either correct or incorrect value in the third table i.e. in compare table. And when the user click the Check Button the compare table value should be display in the message. So, This is our Basic concept to do this project. So let’s do it.

In this tutorials we are gonna learn how you can create quiz website using PHP and MySQL. Let’s start.

Steps:

[sociallocker]

1. If you guyz are working with the PHP for the very first time, then you should know that PHP is server side scripting language, so it executes only in the server. if you create PHP file in your computer and try to run in any browser, there is no output. So working with PHP is far more Different than working in the plain HTML. That’s why you need to install server application in your computer like wamp server, xampp server etc for window.

Download wamp server from here: CLICK HERE

Now you should have text editing software in your PC, every window based PC have Notepad but i am using Dreamweaver CC. After downloading and installing these two software(Dreamweaver and wamp server) you need to define site and testing server in Dreamweaver cc so that every time when you try to execute PHP file, you do not need to save your file by browsing the location of the server. if you don’t know how to do this. Read this article: CLICK HERE

2. After installing wamp server application, Start your wampserver, then open your localhost, for this open up your any browsing software and type localhost or 127.0.0.1 in the address bar. Then new page will appear then scroll down and find phpmyadmin and click on that. Then you will be able to see the login page. Login in by entering username and password (Default username is root and password is empty). And then click on new and create one database having any name you want and the click ok, then create  three table having two field in each table and click on go. After that, you need to insert the field name like: id, qsn, etc. and then select the length as well as data type as your requirement and then click on save. You can watch the video for this designing part, you may understand better in video.

 

3. After completing this task, write the following piece of code and save it in .php extension.


<!doctype html>
<?php
$servername = "localhost";
$username="root";
$password="";
$dbname="quiz1";
$x="correct";
$y="incorrect";

try{
$conn = mysqli_connect($servername, $username, $password, $dbname);
}
catch(MySQLi_Sql_Exception $ex)
{
echo("error in connection");
}
if(isset($_POST['submit'])){
$mteverest=$_POST['mteverest'];
$query=mysqli_prepare($conn, "INSERT INTO `qsn`(`qsn`) VALUES (?)");
mysqli_stmt_bind_param($query, 'd', $mteverest);
$result=mysqli_stmt_execute($query);

try{
if($result){
if(mysqli_affected_rows($conn)>0){
echo("submit successful");
}else{
echo("error in submission");
}

}
}catch(Exception $ex){
echo("error".$ex->getMessage());
}

$querytwo="SELECT qsn.qsn, answer.ans from answer INNER JOIN (SELECT qsn from qsn ORDER BY id DESC LIMIT 1) as qsn on qsn.qsn=answer.ans";
$resultwo=mysqli_query($conn, $querytwo);
if($resultwo){
if(mysqli_affected_rows($conn)>0){
$querythree= mysqli_prepare($conn, "INSERT INTO `compare`(`value1`) VALUES (?)");
mysqli_stmt_bind_param($querythree, 's', $x);
$resultthree=mysqli_stmt_execute($querythree);
}else{
$querythree= mysqli_prepare($conn, "INSERT INTO `compare`(`value1`) VALUES (?)");
mysqli_stmt_bind_param($querythree, 's', $y);
$resultthree=mysqli_stmt_execute($querythree);

}}

}
if(isset($_POST['check'])){
$queryfour="select value1 from compare order by id desc limit 1";
$resultfour=mysqli_query($conn, $queryfour);

if($resultfour){
if(mysqli_num_rows($resultfour)){
while($rows = mysqli_fetch_array($resultfour, MYSQLI_BOTH))
{

echo $rows['value1'] ."<br>";
}
}else{
echo("error in submission");

}

}}
?>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post">
<table align="center">

<tr>
<td>what is hight of mt everest</td></tr>
<tr>
<td></td>

<td><input type="radio" name="mteverest" value="8848">8848</td>
</tr>
<tr>
<td></td>
<td><input type="radio" name="mteverest" value="9848">9848</td>
</tr>
<tr>
<td></td>
<td><input type="radio" name="mteverest" value="10848">10848</td>
</tr>
<tr>
<td></td>
<td><input type="radio" name="mteverest" value="11848">11848</td>
</tr>
<tr>
<tr>
<td><input type="submit" name="check" value="check result"></td>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>

4. Open this .php file in any browser you want. 🙂 That’s it guyz. if you have any confusion you can watch the following videos:

Note: i these video i have use different code which have some security issues, but only for procedure you can watch these video (it does not mean that these video source code is not working it works perfectly but that code is not good when you upload this project to your server because someone can use sql injection technique to inject your sql query 🙂

[/sociallocker]

 

watch the video:

 

 

Show Comments (10)