Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2004, 07:36 AM
Authorized User
 
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Ashleek007
Default PHP count and question check?

What ive got is a quiz style section, where a user logs in and attemps a quiz. The questions are obtained from a database table called kayA_theorytestqns and displayed randomly on the page. This bit works fine.

I then need to check the answer the user has checked against the database correctAnswer field

if the answer is correct i want it to add '1' to the score and then redirect back to the question page so another question is randomly generated. I also want to add '1' to the question number everytime the user answers a question, then when the total number of questions =35 show thier score!

the questions are on theorytest.php and the check page is on questioncheck.php!. I have used a form to submit the value of the chosen answer to the questioncheck.php page.

The code i have used in the questioncheck.php is shown below:-

<?php

     $dbc = mysql_connect('*databasename and password*') or die ('Could not connect to MySQL :' .mysql_error());
     mysql_select_db('*dbname*') or die('Could not connect to database :' .mysql_error());

     session_start();
     $answer = $_POST['answer'];
     $random = $_SESSION['random'];
     $score = '';
     $_SESSION['score'] = $score;
     $qnNumber = '';
     $_SESSION['qnNumber'] = $qnNumber;
    
     $query = "SELECT * FROM kayA_theorytestQns WHERE questionNumber = '$random'";
     $result = mysql_query($query);

if($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    if($answer == $row[correctAnswer])
    {
    $score+1;
    $qnNumber+1;
        if ($qnNumber > 34)
        {
        header('Location: theorytestEND.php');
        }
        else
        {
        header('Location: theorytest.php');
        }
    }
    else
    {
    $score+0;
    $qnNumber+1;
    if ($qnNumber > 34)
        {
        header('Location: theorytestEND.php');
        }
        else
        {
        header('Location: theorytest.php');
        }
    }
    }
else
    {
echo "error in questioncheck.php";
    }
    mysql_close();
?>

cheers for any help again rich and nic
ASH
__________________
My new web design domain
www.askmultimedia.co.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question: check if table exists in DB savoym Java Databases 4 March 9th, 2012 06:57 AM
How to store check box values in php sessions? method PHP How-To 0 February 22nd, 2006 12:08 AM
using php to check file names fogofogo PHP How-To 0 February 20th, 2006 06:15 AM
PHP - Db mysql count dungey PHP Databases 2 July 15th, 2004 02:59 PM
How to count Records using acess as backend in Php manjiri PHP Databases 1 December 23rd, 2003 04:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.