/    Sign up×
Community /Pin to ProfileBookmark

Check answer in database

Hi,

I have PHP forms and a MySQL database.

The user enters their name.

[B]What is your name? [/B] [ FORM ]

What I would then like to do is then ask the user to enter their input into forms – say I ask a question:

[B]What does C in CPU stand for?[/B] [ FORM ]

The answer is stored in the database and if they answer correctly it adds 1 beside the user’s name.

Does anyone know of any code / tutorial that will help me to achieve this?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NoEffinWayFeb 24.2012 — Would you want it real time or to add it after they clicked submit, like a review?
Copy linkTweet thisAlerts:
@mv0010authorFeb 27.2012 — I have a html button - so, after they click submit.

Any ideas??
Copy linkTweet thisAlerts:
@NoEffinWayFeb 27.2012 — Here, this assumes a few things;

1.You are using a session variable to track what user is answering.

2.You have a database for questions in the following format:
[CODE]id|question|answer|choices[/CODE]
3.It assumes these are multiple choice questions

If those things are true, the following code should be plug and play.
[code=php]<?
if(!isset($_COOKIE['question'])){
$expire=time()+60*60*24;
//Use this to track what question they are on.
setcookie('question', 1, $expire);
}elseif(isset($_SESSION['user'])){
//Find out what question they are on.
$qid = $_COOKIE['question'];
if(isset($_POST['submit'])){
$user_answer = $_POST['answer'];
//Simple Sanitation to prevent any modified cookies being passed.
$clean_id = mysql_real_escape_string($_COOKIE['question']);
//Query the Database to get the question
$find_question = mysql_query("SELECT * FROM $db.$questions WHERE id = '$clean_id'");
$question_details = mysql_fetch_array($find_question);
$answer = $question_details['answer'];
if($user_answer == $answer){
$result = 'Correct!';
$user = $_SESSION['user'];
$find_user = mysql_query("SELECT * FROM $db.$users WHERE username = '$user'");
$user_array = mysql_fetch_array($find_user);
$current_points = $user_array['points'];
$current_points++;
$update_points = mysql_query("UPDATE $db.$users SET points = $current_points");
//This is for testing reasons ONLY
if(!$update_points){
$error = 'Could not update points: '.mysql_error();
}
}else{
$result = 'Incorrect!';
}
$_COOKIE['question']++;
}
$get_question = mysql_query("SELECT * FROM $db.$questions WHERE id = ");
//This build the question list
$form = '<form action="?" method="post"><table>';
if(isset($result)){
$form .= '<tr><td>'.$response.'</td></tr>';
}
$form .= '<tr><td>'.$get_question['question'].'</td></tr>';
$muli = explode('|',$get_question['choices']);
$end = count($multi);
$start = 0;
while($start<=$end){
$form .= '<tr><td><input type="radio" name="answer" value="'.$start.'">'.$multi[$start].'</td></tr>';
$start++;
}
$form .= '<tr><td><input type="submit" name="submit" value="Submit" /></td></tr></table></form>';
}
?>

<html><head></head>
<body>
<?=$form;?>
</body>
</html>[/code]
×

Success!

Help @mv0010 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...