mcq test application in php

MCQ test application in php




First of all Thank you very very much For your love and support we have reached 1k Like on FB PAGE so on this occasion i am releasing my new project “TestiFY” an online MCQ test application in php. This MCQ test application has good amount of feature. For this MCQ test application i have used Dirty PHP so that it will be easy for me to explain stuff to you guyz.




MCQ test application in php
Project Structure

MCQ test application in php

Database

Create a database with name testify
Create a table with name test
[sql]
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL,
`name` text NOT NULL,
`marks` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;


— Dumping data for table `test`

INSERT INTO `test` (`id`, `name`, `marks`) VALUES
(1, ‘PHP basics’, 50),
(2, ‘PHP Advance’, 200),
(3, ‘HTML’, 120),
(4, ‘C++’, 150),
(5, ‘Android Basics’, 250);

ALTER TABLE `test`
ADD PRIMARY KEY (`id`);
[/sql]
Create a table with name test_question
[sql]
CREATE TABLE IF NOT EXISTS `test_question` (
`id` int(11) NOT NULL,
`test_id` int(11) NOT NULL,
`question` text NOT NULL,
`option1` text NOT NULL,
`option2` text NOT NULL,
`option3` text NOT NULL,
`option4` text NOT NULL,
`answer` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;


— Dumping data for table `test_question`

INSERT INTO `test_question` (`id`, `test_id`, `question`, `option1`, `option2`, `option3`, `option4`, `answer`) VALUES
(1, 1, ‘What does PHP stand for?’, ‘Hypertext Preprocessor’, ‘pretext Hypertext Processor’, ‘Preprocessor Home Page’, ‘Preprocessor’, ‘1’),
(2, 1, ‘PHP files have a default file extension of..’, ‘.html’, ‘.xml’, ‘.php’, ‘.ph’, ‘3’),
(3, 1, ‘A PHP script should start with ___ and end with ___:’, ‘< php >‘, ‘< ? php ?>‘, ‘‘, ‘‘, ‘4’),
(4, 1, ‘Which of the below statements is equivalent to $add += $add ?’, ‘$add = $add’, ‘$add = $add +$add’, ‘$add = $add + 1’, ‘$add = $add + $add + 1’, ‘2’),
(5, 2, ‘Which version of PHP introduced the advanced concepts of OOP?’, ‘PHP 4’, ‘PHP 5’, ‘PHP 5.3’, ‘PHP 6’, ‘2’),
(6, 2, ‘Which one of the following is the right way to clone an object?’, ‘_clone(targetObject);’, ‘destinationObject = clone targetObject;’, ‘destinationObject = _clone(targetObject);’, ‘destinationObject = clone(targetObject);’, ‘2’),
(7, 2, ‘If one intends to create a model that will be assumed by a number of closely related objects, which class must be used?’, ‘Normal class’, ‘Static class’, ‘Abstract class’, ‘Interface’, ‘3’),
(8, 2, ‘If your object must inherit behavior from a number of sources you must use a/an’, ‘Interface’, ‘Object’, ‘abstract class’, ‘static class’, ‘1’),
(9, 2, ‘Which feature allows us to call more than one method or function of the class in single instruction?’, ‘Typecasting’, ‘Method Including’, ‘Method adding’, ‘Method chaining’, ‘4’),
(10, 2, ‘Which magic method is used to implement overloading in PHP?’, ‘__call’, ‘__invoke’, ‘__wakeup’, ‘__unset’, ‘1’),
(11, 3, ‘A webpage displays a picture. What tag was used to display that picture?’, ‘picture’, ‘image’, ‘img’, ‘src’, ‘3’),
(12, 3, ‘ tag makes the enclosed text bold. What is other tag to make text bold?’, ‘‘, ‘‘, ‘‘, ‘‘, ‘1’),
(13, 3, ‘Tags and test that are not directly displayed on the page are written in _____ section.’, ‘‘, ‘‘, ‘‘, ‘<body>‘, ‘2’),<br /> (14, 3, ‘Which tag inserts a line horizontally on your web page?’, ‘</p> <hr>‘, ‘<line>‘, ‘<line direction=”horizontal”>‘, ‘</p> <tr>‘, ‘1’),<br /> (15, 3, ‘What should be the first tag in any HTML document?’, ‘<head>‘, ‘<title>‘, ‘<html>‘, ‘<document>‘, ‘3’),<br /> (16, 3, ‘Which tag allows you to add a row in a table?’, ‘</p> <td> and </td> <p>‘, ‘<cr> and </cr>‘, ‘</p> <th> and </th> <p>‘, ‘</p> <tr> and </tr> <p>‘, ‘4’),<br /> (17, 3, ‘How can you make a bulleted list?’, ‘<list>‘, ‘<nl>‘, ‘</p> <ul>‘, ‘</p> <ol>‘, ‘3’),<br /> (18, 4, ‘What is data hiding ?’, ‘It is related with hiding internal object details’, ‘It is related with showing internal object details’, ‘It is related with datatypes’, ‘None of above’, ‘1’),<br /> (19, 4, ‘What is class in c++ ?’, ‘When you define a class, you define a blueprint for a data type.’, ‘When you define a class, you make get more funtionality.’, ‘When you define a class, you define the logic.’, ‘When you define a class, you make debugging.’, ‘1’),<br /> (20, 4, ‘What is object in C++ ?’, ‘Object is part of syntax of a class.’, ‘Object is datatype of a class.’, ‘Object is an instance of a class.’, ‘Object is function of a class.’, ‘3’),<br /> (21, 4, ‘What is purpose of abstract class ?’, ‘to provide help with database connectivity.’, ‘to provide data input to other classes.’, ‘to provide security to other classes.’, ‘to provide an appropriate base class from which other classes can inherit.’, ‘4’),<br /> (22, 4, ‘What is default visibility mode for members of classes in C++ ?’, ‘Private’, ‘Public’, ‘Protected’, ‘Depends’, ‘1’),<br /> (23, 5, ‘Once installed on a device, each Android application lives in_______?’, ‘device memory’, ‘external memory’, ‘security sandbox’, ‘None of the above’, ‘3’),<br /> (24, 5, ‘Parent class of Activity?’, ‘Object’, ‘Context’, ‘ActivityGroup’, ‘ContextThemeWrapper’, ‘4’),<br /> (25, 5, ‘What are the indirect Direct subclasses of Activity?’, ‘LauncherActivity’, ‘PreferenceActivity’, ‘TabActivity’, ‘All the above’, ‘4’),<br /> (26, 5, ‘Parent class of Service?’, ‘Object’, ‘Context’, ‘ContextWrapper’, ‘ContextThemeWrapper’, ‘3’),<br /> (27, 5, ‘What are the indirect Direct subclasses of Services?’, ‘RecognitionService’, ‘RemoteViewsService ‘, ‘SpellCheckerService’, ‘InputMethodService’, ‘4’);</p> <p>ALTER TABLE `test_question`<br /> ADD PRIMARY KEY (`id`);<br /> [/sql]</p> <p>create a table with name <strong>test_taken</strong><br /> [sql]<br /> CREATE TABLE IF NOT EXISTS `test_taken` (<br /> `id` int(11) NOT NULL,<br /> `test_id` int(11) NOT NULL,<br /> `user_id` int(11) NOT NULL,<br /> `total` int(11) NOT NULL,<br /> `marks_obtain` int(11) NOT NULL<br /> ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;</p> <p>—<br /> — Dumping data for table `test_taken`<br /> —</p> <p>INSERT INTO `test_taken` (`id`, `test_id`, `user_id`, `total`, `marks_obtain`) VALUES<br /> (1, 1, 1, 50, 50),<br /> (2, 5, 1, 250, 150),<br /> (3, 4, 2, 150, 90),<br /> (4, 2, 2, 200, 67),<br /> (5, 3, 2, 120, 34),<br /> (6, 1, 2, 50, 13),<br /> (7, 5, 2, 250, 0),<br /> (8, 4, 1, 150, 150),<br /> (9, 1, 4, 50, 50),<br /> (10, 2, 4, 200, 67);</p> <p>ALTER TABLE `test_taken`<br /> ADD PRIMARY KEY (`id`);<br /> [/sql]</p> <p>Create a table with name <strong>user</strong><br /> [sql]<br /> CREATE TABLE IF NOT EXISTS `user` (<br /> `id` int(11) NOT NULL,<br /> `username` text NOT NULL,<br /> `password` text NOT NULL<br /> ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;</p> <p>—<br /> — Dumping data for table `user`<br /> —</p> <p>INSERT INTO `user` (`id`, `username`, `password`) VALUES<br /> (1, ‘husain’, ‘test123’),<br /> (2, ‘ali’, ‘test123’),<br /> (3, ‘murtuza’, ‘test123’),<br /> (4, ‘hunk’, ‘test123’);</p> <p>ALTER TABLE `user`<br /> ADD PRIMARY KEY (`id`);</p> <p>— AUTO_INCREMENT for table `test`<br /> —<br /> ALTER TABLE `test`<br /> MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6;<br /> —<br /> — AUTO_INCREMENT for table `test_question`<br /> —<br /> ALTER TABLE `test_question`<br /> MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=28;<br /> —<br /> — AUTO_INCREMENT for table `test_taken`<br /> —<br /> ALTER TABLE `test_taken`<br /> MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=11;<br /> —<br /> — AUTO_INCREMENT for table `user`<br /> —<br /> ALTER TABLE `user`<br /> MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5;<br /> [/sql]</p> <h2>inc/connect.inc.php</h2> <p>In this file we are connecting to the database<br /> [php]<br /> <?php $con = mysqli_connect("localhost","root","","Testify"); if(mysqli_connect_errno()){ echo mysqli_connect_error(); } ?><br /> [/php]</p> <h2>inc/header.inc.php</h2> <p>in header we are including the <strong>connect.inc.php</strong> file<br /> [html]<br /> <?php require_once("connect.inc.php"); session_start(); ?><br /> <html><br /> <head><br /> <title>Online Test Application with PHP and Mysqli


[/html]

inc/footer.inc.php

[html]


[/html]



register.php

  1. One line 4 we are checking if user is loggedin we will send him to index.php
  2. From 23 we are waiting for register button to be clicked and then we are performing some check and storing the user info in `user` table

[php]

Register


Fill in all the fields

“;
exit();
}

//check user is unique
$unique = mysqli_query($con,”SELECT id FROM `user` WHERE username=’$username'”);
if(mysqli_num_rows($unique) != 0){
echo “

Username {$username} already registered.

“;
exit();
}

//check password length
if(strlen($password) < 6){ echo "

Password should be more the 6 characters

“;
exit();
}

//insert into db
$q = mysqli_query($con,”INSERT INTO `user` VALUES(”,’$username’,’$password’)”);

if($q){
echo “

Registered

“;
$_SESSION[‘id’] = mysqli_insert_id($con);
$_SESSION[‘username’] = $username;
header(“Location: index.php”);
exit();
}else{
echo “

Failed to register user

“;
exit();
}
}
?>


[/php]

login.php

  1. on line 4 we are checking if user is loggedin send him to index.php
  2. on line 23 we are waiting for login button to be pressed
  3. On line 28 we are checking if username or password is empty so that we can so error message
  4. On line 34 we are fetching user information from the database

[php]

Login


Fill in all the fields

“;
exit();
}

//check username and password match the db record
$q = mysqli_query($con,”SELECT id FROM `user` WHERE username=’$username’ AND password=’$password'”);
if(mysqli_num_rows($q) != 1){
echo “

Invalid username or password

“;
exit();
}

//fetch the if of the loggedin user start the session
$row = mysqli_fetch_assoc($q);
//set the session with loggedin user id
$_SESSION[‘id’] = $row[‘id’];
$_SESSION[‘username’] = $username;
header(“Location: index.php”);
exit();
}
?>