PHP adding to sessions - php

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";
}

Related

Data is still kept and won't restart back from zero

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']);
}
}
?>

Why is 400030 not giving out the logic that its greater than 400000?

I wrote a code to echo "limit reached" if A is greater than B. But if A is 400030 and B is 400000 it shows no output. If A is further greater than that, let say 400060 or any number higher than that, it shows the output.. Please how do I explain that? The code snippet to demonstrate what I mean is....
<?php
include_once('db.php');
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if(!isset($_SESSION['login'])) {
echo ("<script>location.href='../clogin/'</script>");
die();
}
if(isset($_POST['transfer'])) {
$username = $_SESSION['login'];
$transAmount = $_POST['transAmount'];
$totalTrans = $transAmount + 30;
$sql = "SELECT * FROM customer WHERE username = ?";
$stmt = $connection->prepare($sql);
$stmt->bind_param('s', $username);
$stmt->execute();
$result = $stmt->get_result();
if(!$result) {
die('ERROR:' . mysqli_error($connection));
}
$count = $result->num_rows;
if($count == 1) {
while ($row = $result->fetch_assoc()){
$accTrans = $totalTrans + $row['dailyTrans'];
$sql2 = "UPDATE customer set dailyTrans=? WHERE username=?";
$stmt = $connection->prepare($sql2);
$stmt->bind_param('is', $accTrans,$username);
$stmt->execute();
if(!$stmt) {
die('network problem');
}
if($row['dailyTrans'] >= $row['dailyLimit']) {
echo '<script>swal.fire("FAILED!!", "<strong>You have reached the total amount you can send per day.</strong><hr><br/><i>Visit your bank to increase transfer limit.</i>", "error");
window.setTimeout(function(){
window.location.href = "transfer1.php";}
, 1700);
</script>';
//exit();
} else {
echo"";
}
}//while loop
}//count
}//submit
?>
My question Summary Again
The value for $row['dailyTrans'] is 400030 and the value of $row['dailyLimit'] is 400000
This is suppose to echo out the error but fails... if $row['dailyTrans'] is greater than 400030, it echoes out. What is the logic behind that?.
Please be nice with your comments as usual. Thanks . Both Value are integers!!
Looks to me that you are adding $_POST['transAmount'] after querying the database for the user.
To get the current dailyTrans you will need to either
Select from customer again
(fetching the updated dailyTrans)
OR
compare dailyLimit to $accTrans ::
(if ($accTrans >= $row['dailyLimit'])

Is there a way of returning back the question and answer from the database not done correctly in a test by the user?

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
}

Online Offline status for mysql messaging system -PHP MySQL

I have looked everywhere and cannot seem to find out the answer to fix my problem.
I have an online messaging system and i would like to implement Online and Offline status to the users. But no matter what i do with this code... I cannot seem to get it working. Any help would be greatly appreciated. Keep in mind I dont want to alter any of my existing code other than the ones that have to do with $online and the q2 variables.
my code is:
<?php
//show all the users expect me
$q = mysqli_query($con, "SELECT * FROM `users` WHERE id!='$user_id'");
$q2 = mysqli_query($con, "SELECT status FROM `users`");
//display all the results
while($row = mysqli_fetch_assoc($q2)){
$online = $row['status'];
if($online == "online"){
$tcolor = "green";
}
elseif($online=="offline"){
$tcolor = "red";
}
elseif($online ==""){
$tcolor= "gray";
}
}
while($row = mysqli_fetch_assoc($q)){
echo "<a href='/users/pm?id={$row['id']}'><li style='border-left:{$tcolor} 15px solid;'><img src='{$row['pic']}'> {$row['name']}</li></a>";
}
?>
Your logic for setting $online is reversed. It should be: $online = $row['status'];
EDIT BELOW:
Your original $q2 was unnecessary as it was just retrieving the status column and nothing was linked to the users. Also, you have all the data you need in $q.
<?php
//show all the users expect me
$q = mysqli_query($con, "SELECT * FROM `users` WHERE id!='$user_id'");
//display all the results
while($row = mysqli_fetch_assoc($q)){
$online = $row['status'];
if($online == "online"){
$tcolor = "green";
}
elseif($online=="offline"){
$tcolor = "red";
}
elseif($online ==""){
$tcolor= "gray";
}
echo "<a href='/users/pm?id={$row['id']}'><li style='border-left:{$tcolor} 15px solid;'><img src='{$row['pic']}'> {$row['name']}</li></a>";
}
?>

PHP MYSQL Get data from database and echo message if no data retrieved

Im trying to make a code where it displays all names in database where "clanwars" is set to 1 (int) and if all of them is 0 echo an message like: "Noone has signed up yet!"
This is some of the code i have:
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
while($row = mysqli_fetch_array($result))
{
if ($row['clanwars'] != '0') {
echo $row['mantra']."<br>";
} else {
echo 'Noone has signed up yet!';
}
}
mysqli_close($con);
First, in your example row['clanwars'] will never equal to 0 because you already specified WHERE clanwars = 1 in your query, so MySQL will return only those that have clanwars=1. If I understand well, you need to do something like:
<?
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
if (mysqli_num_rows($result)==0) echo 'Noone has signed up yet';
else {
while ($row = mysqli_fetch_array($result)) {
//do what you need
}
}
?>
So basically, you retrieve everyone who has CLANWARS set to 1 in the database. If there are records, process them, if there are no records, it means that nobody has signed up.
Is this what you need?
Try this:
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
if (mysqli_num_rows($result) == 0)
{
echo 'Noone has signed up yet!';
}
else
{
while ($row = mysqli_fetch_array($result))
{
echo $row['mantra']."<br>";
}
}
mysqli_close($con);

Categories