Publishing System Settings Logout Login Register
Create a simple php quiz system
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on July 8th, 2010
14947 views
PHP Coding

In this tutorial I am going to teach you how to create a simple quizsystem. I saw that in these days, a lot of websites have this type ofthings. In this way, you can keep a track of what your users desires, soyou can implement it in the following versions of your site. Thisscript can also be implemented in any type of php script.

 

First of all, you will need to create a database in your phpmyadmin andthen run the following sql command:

 

CREATE TABLE `quiz` (
`id` mediumint(8) NOT NULL auto_increment,
`questions` varchar(255) NOT NULL default '',
`answers` varchar(255) NOT NULL default '',
PRIMARY KEY(`id`),
UNIQUE(`questions`)
) ENGINE=MyISAMDEFAULT CHARSET=utf8;

CREATE TABLE `money` (
`id` int(20) NOT NULL auto_increment,
`money` varchar(250) NOT NULL,
`data` text NOT NULL,
PRIMARY KEY(`id`)
) ENGINE=MyISAMDEFAULT CHARSET=utf8; 

 

Next, go into your config.php file and copy thefollowing code:

 

<?php
session_start();
error_reporting(0);
// Database Information
 $AdresaBazaDate = "localhost";// DB ADDRESS ( usually localhost )
 $UtilizatorBazaDate = "";// DB USER
 $ParolaBazaDate = "";// DB PASS
 $NumeBazaDate = "";// DB NAME

 $conexiune = @mysql_connect($AdresaBazaDate,$UtilizatorBazaDate,$ParolaBazaDate) or die("Nu ma pot conecta la MySQL!");
 @mysql_select_db($NumeBazaDate, $conexiune) or die("Nu gasesc baza de date ".$db."!");
 @mysql_close($AdresaBazaDate,$UtilizatorBazaDate,$ParolaBazaDate);
?>

 

I assume that you already know a little bit of php since you are a socalled “webmaster” and I am not going to explain the code. If you needhelp just drop me a line through the contact form.

Next we are going to wrap up the index.php file:

 

<?php
include("config.php");
?>
<?php
$sql = "SELECT * FROM quiz ORDER BY rand() LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
$question = "$row->questions";
$correctanswer = "$row->answers";
$money = '20';
$today = date('Y-m-d');
if (isset($_POST['submitted'])) {
if (empty($_POST['answer']) || empty($_POST['id'])) {
echo '

You must enter an answer!

'; } else { $answer = $_POST['answer']; $sql_answer = "select * from quiz where id = '".addslashes($_POST["id"])."'"; $result1 = mysql_query($sql_answer); $row1 = mysql_fetch_object($result1); $correctanswer = $row1->answers; if (strtolower($answer) == strtolower($correctanswer)) { echo 'Congratulations, your answer was correct!'; $sql3= "INSERT INTO money VALUES ('','$money','$today')"; $res3 = mysql_query($sql3); } elseif (strtolower($answer) != strtolower($correctanswer)){ echo 'Bad answer! The correct answer was '.$correctanswer.'.'; } } } ?>
<?php echo "Question: $question"; ?> Answer:

Also, if you want to have a top.php file so you canshow for example how much money was raised, just use the following code:

 

<?php
include ("config.php");
?>
<?php
$query1="SELECT money, data, Sum(money) AS SumOfMoney FROM money GROUP BY DATE_FORMAT(data, '%M %d, %Y') = NOW()";
$result1=mysql_query($query1);
$row1=mysql_fetch_assoc($result1);
echo 'Total: '.$row1['SumOfMoney'].' dollars

';
?>

There you go, you have created a sexy simple quiz system. The only thingis that you have to manually put your questions with your answers inyour database. In the future maybe I will develop a better version ofthis script.

Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
zuwebmasters

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top