I have a quiz system where if the correct answer is chosen, the score will +1. But I clicked backspace once, and the score won't reset to 0 everytime I backspace. I tried re-answering the question, but the score is still kept and won't start back from 0
keep in mind tho i used 'skor' instead of 'score'
<?php
require 'connect.php';
//GET SUBJECT ID
session_start();
$topikpilihan=$_SESSION['topikpilihan'];
?>
<?php session_start(); ?>
//CHECK SCORE
<?php
if (!isset($_SESSION['skor'])){
$_SESSION['skor'] = 0;
}
//WHEN THE SELECTED ANSWER IS POSTED
if($_POST){
$idsoalan = $_POST['idsoalan'];
$number = $_POST['number'];
$selected_choice = $_POST['pilihan'];
$nextsoalan=$number+1;
$totalsoalan=4;
//GET TOTAL QUESTIONS
$query="SELECT * FROM soalan where idtopik= '$topikpilihan'";
$result1 = mysqli_query($condb,$query);
$totalsoalan=mysqli_num_rows($result1);
//GET CORRECT ANSWER
$query2 = "SELECT * FROM soalan WHERE nosoalan = $number AND idsoalan=$idsoalan";
$result2 = mysqli_query($condb,$query2);
$row = mysqli_fetch_assoc($result2);
$correct_choice=$row['jawapansoalan'];
//COMPARE SELECTED ANSWER AND CORRECT ANSWER
if($correct_choice == $selected_choice){
$semakan="TEPAT";
$_SESSION['skor']++;
}
if($number == $totalsoalan){
header("Location: jawabsoalan-tamat.php");
exit();
} else {
header("Location: jawabsoalan-mula.php?semakan=".$semakan."&idtopik=".$topikpilihan."&n=".$nextsoalan."&skor=".
$_SESSION['skor']);
}
}
?>
Related
I am writing a short php quiz quizzer. For the question not done correctly, i want the code to be able return the question number and supposed answer and for the correct question i want to increase the score
So far the coding is working but when i tried to add the number of the incorrect question and its correct answer to an array $wrong_answer i got stack and can't figure out the way around
<?php include 'database.php'; ?>
<?php session_start(); ?>
<?php
//Check to see if score is set_error_handler
if (!isset($_SESSION['score'])){
$_SESSION['score'] = 0;
}
//Check if form was submitted
if($_POST){
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next=$number+1;
$wrong_answer = array();
$lesson = (int) $_GET['l'];
//Get total number of questions
$query = "select * from questions where lesson_number = $lesson";
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total=$results->num_rows;
//Get correct choice
$q = "select * from `choices` where question_number = $number and is_correct=1";
$result = $mysqli->query($q) or die($mysqli->error.__LINE__);
$row = $result->fetch_assoc();
$correct_choice=$row['id'];
//compare answer with result
if($correct_choice == $selected_choice){
$_SESSION['score']++;
}else {
foreach($number and $correct_choice){
$wrong_answer [] = $number, $correct_choice;
}
}
if($number == $total){
header("Location: final.php");
exit();
} else {
header("Location: B.php?n=".$next."&l=$lesson&score=".$_SESSION['score']);
}
}
?>
why are you writing a foreach loop when the variable isn't an array?
did you mean to do
if($correct_choice == $selected_choice){
$_SESSION['score']++;
}else {
$wrong_answer=[$number,$correct_choice];
}
or
if($correct_choice == $selected_choice){
$_SESSION['score']++;
}else {
array_push($wrong_answer,[$number, $correct_choice]);//for 2d array
}
I have a two forms from one form. I can sucessfully store the data to database.when that form submitted user will directed to the second form. I am passing variable $uniqueid in the url from first form to second form. But, when I tried stored the data of the second form into the database that relevant to the same user its not stored.
I want to store mobile number of the user from second page.databse column also mobile number.
This is my code
<?php
include_once 'dbconnect.php';
$a = $_GET['uniquekey'];
if(isset($_POST['btn-signup']))
{
$mobilenumber = $_POST['mobilenumber'];
$xxx = mysql_query("SELECT * FROM who WHERE uniquekey = '$a'")or die(mysql_error());
$yyy = mysql_fetch_row($xxx);
if(mysql_num_rows($xxx) > 0) {
$aaa = mysql_query("INSERT INTO who(mobilenumber) VALUES('$mobilenumber')");
}
else{
echo 'wrong';
}
}
?>
$xxx = mysql_query("SELECT * FROM who WHERE uniquekey = '$a'")or die(mysql_error());
$yyy = mysql_fetch_row($xxx);
if(mysql_num_rows($xxx) > 0) {
$aaa = mysql_query("UPDATE who setmobilenumber='$mobilenumber' where uniquekey = '$a' ");
}
else{
echo 'wrong';
}
Here you can use update query for update user mobile number.
include_once 'dbconnect.php';
$a = $_GET['uniquekey'];
if(isset($_POST['btn-signup']))
{
$mobilenumber = $_POST['mobilenumber'];
$xxx = mysql_query("SELECT * FROM who WHERE uniquekey = '$a'")or die(mysql_error());
$yyy = mysql_fetch_row($xxx);
if($yy>0)
{
$update="update who set mobilenumber=$mobilenumber where uniquekey='$a'";
$query=mysql_query($update);
}
else
{
echo "wrong";
}
}
This is my code in 1st page,
<?php
session_start();
require 'dataconnection.php';
$res = mysql_query("select * from questions where category_id=1 LIMIT 20") or die(mysql_error());
$rows = mysql_num_rows($res);
echo $rows;
while ($result=mysql_fetch_assoc($res)) {
for($i=1;$i<=$rows;$i++)
{
$_SESSION['questions']=$result;
}
echo implode("",$_SESSION['questions']);
}
?>
In next page my code
<?php
session_start();
echo implode(",",$_SESSION['questions']);
?>
This part of the code doesn't add to an array, but overwrite the value in it. It simply sets $_SESSION['questions'] to the same value as $result x times, where x is the number of rows.
while ($result=mysql_fetch_assoc($res)) {
for($i=1;$i<=$rows;$i++)
{
$_SESSION['questions']=$result;
}
}
Try changing it to:
while ($result=mysql_fetch_assoc($res)) {
$_SESSION['questions'][]=$result;
}
I am making an online quiz, in this quiz, one question is asked at a time. If the answer is correct, I want to add that point, to the total score earned. I want the $_SESSION to show how many points the user has earned so far. The code I have so far is this:
$sql = "SELECT * FROM quiz_questions WHERE Question='$question' AND Answer='$answer'";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if ($num_rows != 0){
$score++;
$total_score += $score;
session_start();
$_SESSION['score'] = $total_score;
} else {
echo "Incorrect";
}
}
I already have $score equal to 0 to start in the beginning of the code. Thank you in advance for all your help!
session_start(); // should be at the beginning of your script(s);
if (!isset($_SESSION['score'])) { $_SESSION['score'] = 0; }
$sql = "SELECT * FROM quiz_questions WHERE Question='$question' AND Answer='$answer'";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if ($num_rows != 0){
// $row = fetch row to get the actual score for this question, if answered right?
$row = mysql_fetch_row($result);
//$score++; ??????
//$total_score += $score;
$_SESSION['score'] += $row['score_index'];
//$_SESSION['score']++; // or just this, dunno
echo 'Your current score is: '.$_SESSION['score'];
}
else {
echo "Incorrect";
}
I'm really struggling with this now for a while and can't seem to get it working. In members.php (where I show all the registered users) I have a list printed out with a link "ADD TO FRIENDS" next to each user.
I managed, for testing purposes to display each members id well (so it gets the ID) but when I click the link it directs to the friends.php where it seems the fault is in. I don't know how to get that friend's id I clicked on IN THE friends.php file. Please have a look!
members.php
<?php
include 'connect.php';
include 'header.php';
if(isset($_SESSION['signed_in']) == false || isset($_SESSION['user_level']) != 1 )
{
//the user is not an admin
echo '<br/>';
echo 'Sorry! You have to be <b>logged in</b> to view all the <b>registered</b> members.';
echo '<br/><br/>';
}
else
{
echo '<h2>Registered users:</h2>';
$sql = "SELECT * FROM users ORDER BY user_name ASC";
$result = mysql_query($sql);
$num=mysql_numrows($result);
$i=0;
while ($i < $num)
{
//$name = mysql_result($result,$i,"user_name");
//$id = mysql_result($result,$i,"user_id");
//$picture = mysql_result($result,$i,"pic_location");
//?friend_id="'. $id .'
while($user = mysql_fetch_array($result)){
echo $user['user_name'].'<br/><br/>ADD TO FRIENDS<br/>';
echo $user['user_id'];
echo '<br/><br/>';
}
$i++;
}
///////////////////////////////
/// adding users as friends ///
///////////////////////////////
//while($user = mysql_fetch_array($result))
//echo $user['user_name'].'
//ADD TO FRIENDS<br/>';
//NOW I WANT TO MAKE A SPECIFIC "ADD AS FRIEND" LINK NEXT TO EACH USER
}
include 'footer.php';
?>
As I said I'm not sure how to get this so please have a look! Thanks!
J
friends.php
<?php
include "connect.php";
include "header.php";
if(isset($_SESSION['signed_in']) == false || isset($_SESSION['user_level']) != 1 )
{
//the user is not an admin
echo '<br/>';
echo 'Sorry! You have to be <b>logged in</b> if you want to add the person as a friend!';
echo '<br/><br/>';
}
else
{
$sql = "SELECT * FROM users";
$result = mysql_query($sql);
//friend_id is the ID of the friend that is clicked on...
//HOW DO I GET THAT ID THAT IS CLICKED ON IN THE WHILE "loop" in members.php?
$friends = ("INSERT INTO friends SET user_id='" . $_SESSION['user_id'] . "', friend_id='".$id."', status='0'");
$result_friends = mysql_query($friends);
if(!$friends)
{
//When you can't add this person as a friend this error will show!
echo 'You cannot add this user at this time. Please try again later!';
}
else
{
//When the friend is now added to the system!
echo 'Great! Now the user needs to approve before you can be friends!';
}
}
?>
On your friends.php use
$_GET['user_id']
Instead of $id, $id is undefined, to get the value of id from the query string you call it using an $_GET variable like,
$_GET['name_of_query_string_value']