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:

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="websitequiz";
$x="correct";
$y="incorrect";

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

try{
//$result = mysqli_stmt_execute($query);

if($result){
if(mysqli_affected_rows($conn)>0){
echo "submitted successfully";
}
else{
echo "error in submission";
}
}
}catch(Exception $ex){
echo ("error in connection");
}
$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";
$resulttwo = mysqli_query($conn, $querytwo);
if($resulttwo){
if(mysqli_affected_rows($conn)>0){
$querythree= mysqli_prepare($conn, "INSERT INTO `compare`(`value`) VALUES (?)");
mysqli_stmt_bind_param($querythree, 's', $x);
$resultthree=mysqli_stmt_execute($querythree);
}else{
$querythree= mysqli_prepare($conn, "INSERT INTO `compare`(`value`) VALUES (?)");
mysqli_stmt_bind_param($querythree, 's', $y);
$resultthree=mysqli_stmt_execute($querythree);
}

}
}
if(isset($_POST['check'])){
$queryfour = "select value 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, MYSQL_BOTH)){
echo($rows['value']."<br>");

}

}
}else{
echo "eroor in result";
}
}



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

<body>
<form action="" method="post">
<table align="center">
<tr>
<td>What is the height 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="1048">10848</td>
</tr>
<tr>
<td></td>
<td><input type="radio" name="mteverest" value="11848">11848</td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></td>
<td><input type="submit" name="check" value="check result"></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 🙂

 

 

SHARE