my code is,
$query3 = "SELECT * FROM db_exam_skip WHERE user='$session'";
$result3 = mysql_query($query3) or die(mysql_error());
$length3 = mysql_num_rows($result3);
while($rows3 = mysql_fetch_array($result3))
{
$query1 = "SELECT * FROM db_exam_questions WHERE id='$rows3[ques_id]'";
$result1 = mysql_query($query1) or die(mysql_error());
$length1 = mysql_num_rows($result1);
}
if(isset($_POST['next']))
{
if(isset($_SESSION['list']))
{
mysql_data_seek($result1,$_SESSION['list']);
}
else
{
$list = $_POST['list'];
mysql_data_seek($result1,$list);
}
}
<?php
while($rows1 = mysql_fetch_row($result1))
{
$start = $rows1[0];
$_SESSION['start'] = $start;
?>
<form action="" method="post">
<p style="font-size:20px;font-weight:bold"><?php echo $rows1[5]; ?></p>
<ul style="list-style-type:none">
<input type='hidden' name='number' value='<?php echo $_SESSION['order']++; ?>' />
<input type='hidden' name='list' value='<?php echo $_SESSION['list']++; ?>' />
<input type='hidden' name='ques_id' value='<?php echo $rows1[0]; ?>' />
<input type='hidden' name='correct' value='<?php echo $rows1[10]; ?>' />
<li><input type="radio" name="answer" value="1" /> <?php echo $rows1[6]; ?> <br><br>
<input type="radio" name="answer" value="2" /> <?php echo $rows1[7]; ?> <br><br>
<input type="radio" name="answer" value="3" /> <?php echo $rows1[8]; ?> <br><br>
<input type="radio" name="answer" value="4" /> <?php echo $rows1[9]; ?> <br></li>
</ul>
<input type="submit" class="button4" value="Next" name="next" />
</form>
<?php
break;
}
?>
my question is, after the first question is loaded, when i press next button to load the second question I am getting the below error.
Warning: mysql_data_seek(): Offset 2 is invalid for MySQL result index 8 (or the query data is unbuffered) in C:\wamp\www\Albert\ICAMS\start_skip_question.php on line 15
I have tried a lot to solve this error. But till now no success. Is there any method to solve. Any help will be appreciated.
Thank you.
I guess the result set is empty.I think query is returning empty set.
You will get this error if result set is empty
check the PHP DOCS
First check if you are getting ant rows from the result
if (mysqli_num_rows($sql) > 0)
{
}
Related
Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="post">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
Its html page to ask question
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label for='.$question_body.'>Yes</label>
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="'.$question_id.'" value="No">
<input type="hidden" name="option_b" value="'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
}
}
?>
Basically this form asked question whether yes or no using radio button. $option_a == 'Yes' and $option_b == 'No'. The question is like this "Are you have fever ?". So when i submit the value did not pass to the 'Answer.php' page.
'Answer.php' page.
<?php
include 'conn.php';
if(isset($_POST['Submit']) && !empty($_POST['Submit'])){
echo $_POST['option_a'];
echo 'succeed';
}
else{
echo 'no data';
}
?>
In this page have error undefined_index value but still echo succeed.
Your HTML code should look like the following:
<input type="radio" name="whatevername" value="Yes">
<input type="hidden" name="whatevername" value="No">
You can use PHP to insert whatever values you want but you need the radio button name to be the same.
Then if you want to echo that in PHP you'd use:
echo $_POST['whatevername']; //same name you used in the form
You are passing value as name for radio buttons
name="'.$option_a.'"
This will result you in name ="Yes"
And you are trying to fetch echo $_POST['option_a']; where option_a is not defined.
Try this
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
Same for other radio button
Try this one :
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
echo '<div class="A4">';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<div class="A4">
<h2 class="qtitle">'.$question_body.'</h2>
<form action="Answer.php" method="post">
<label for='.$question_body.'>Yes</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
echo'
<input type="submit" name="Submit" value="Submit">
</form>
</div>';
}
}
?>
<?php
include 'conn.php';
if(isset($_POST['submitted']) && !empty($_POST['submitted'])){
$questionAndOptions = $_POST['radioQuestions'];
foreach ($questionAndOptions as $questionAndOption) {
$arrQuestionAndOption = explode("-", $questionAndOption);
echo $arrQuestionAndOption[0]; //question
echo $arrQuestionAndOption[1]; //option
}
echo 'succeed';
}
else{
echo 'no data';
}
?>
i am creating a quiz system same like http://www.sqlquiz.com/ and i have problem in the result page.
i have created 4 pages
index.php ---->welcomePage where i use a varible n in query string
index.php
<p>Start SQL Quiz</p>
2.quizmain.php------------>this page shows questions and their options and after
getting responce for each question it will go to the process.php page where the score is calculate and every time the counter is incremented by 1 so that after reaching 10th question the final result page will be displayed
quizmain.php
<?php
session_start();
require_once("connection.php");
extract($_REQUEST);
$number = (int) $_GET['n']; //starting value 1
echo $number;
$n1=rand(1,100);
$_SESSION['RQuestionNumber']=$n1;
$q=mysql_query("select * from quiz WHERE qno = '".$n1."'");
$a=mysql_fetch_array($q);
echo $a['qno'];
echo $a['ans'];
?>
<body>
<table>
<tr>
<td width="757" height="390"><div align="center">
<form method="post" action="process1.php">
<input type="radio" name="question" value="a" />
<?php echo $a[2]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="b" />
<?php echo $a[3]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="c" />
<?php echo $a[4]?></label>
</p>
<p><br />
<label> </label>
<label>
<input type="radio" name="question" value="d" />
<?php echo $a[5]?></label>
</p>
<p> </p>
<p>
<label>
<input type="submit" value="Submit" name="Submit" />
<input type="hidden" name="number" value="<?php echo $number; ?>" />
</form>
</label>
<br />
<br />
<br />
</p></td>
</tr>
</table>
</body>
</html>
process.php
<?php
session_start();
require_once('connection.php');
extract($_REQUEST);
//Check to see if score is set_error_handler
if(!isset($_SESSION['score'])){
$_SESSION['score'] = 0;
}
if($_POST)
{
$number = $_REQUEST['number']; //value of number is 1 initially
$selected_choice = $_REQUEST['question'];
$next = $number+1; //
$total=10;
$_SESSION['RQuestionNumber'];
$q = mysql_query("SELECT ans FROM quiz WHERE qno = '".$_SESSION['RQuestionNumber']."'");
$result=mysql_fetch_array($q);
//$store=array();
//Compare
if($result[0][0] == $selected_choice){
//Answer is correct
$_SESSION['score']++;
}
//Check if last question
if($number == $total){
header("location: resultTable.php");
exit();
} else {
header("location: quizmain.php?n=".$next); //now the value of n is 2
}
}
?>
resultTable.php
now in this page i want to print all the questions which appear during the quiz (Same set of questions) along with the marked answer and correct ans
i have tried using session variables but it don't work.
<?php session_start(); ?>
<p>Final Score: <?php echo $_SESSION['score']; ?></p>
<?php session_destroy(); ?>
You session value will be override each time so at last you will get last questtion id. If you want all question Ids then use array. Try this:
quizmain.php
<?php
session_start();
require_once("connection.php");
extract($_REQUEST);
$number = (int) $_GET['n']; //starting value 1
echo $number;
$n1=rand(1,100);
$_SESSION['RQuestionNumber'][]=$n1;
$q=mysql_query("select * from quiz WHERE qno = '".$n1."'");
$a=mysql_fetch_array($q);
echo $a['qno'];
echo $a['ans'];
?>
<body>
<table>
<tr>
<td width="757" height="390"><div align="center">
<form method="post" action="process1.php">
<input type="radio" name="question" value="a" />
<?php echo $a[2]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="b" />
<?php echo $a[3]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="c" />
<?php echo $a[4]?></label>
</p>
<p><br />
<label> </label>
<label>
<input type="radio" name="question" value="d" />
<?php echo $a[5]?></label>
</p>
<p> </p>
<p>
<label>
<input type="submit" value="Submit" name="Submit" />
<input type="hidden" name="number" value="<?php echo $number; ?>" />
</form>
</label>
<br />
<br />
<br />
</p></td>
</tr>
</table>
</body>
</html>
process.php
<?php
session_start();
require_once('connection.php');
extract($_REQUEST);
//Check to see if score is set_error_handler
if(!isset($_SESSION['score'])){
$_SESSION['score'] = 0;
}
if($_POST)
{
$number = $_REQUEST['number']; //value of number is 1 initially
$selected_choice = $_REQUEST['question'];
$next = $number+1; //
$total=10;
$last_que = $_SESSION['RQuestionNumber'][count($_SESSION['RQuestionNumber'])-1];
// $_SESSION['RQuestionNumber'];
$q = mysql_query("SELECT ans FROM quiz WHERE qno = '".$last_que."'");
$result=mysql_fetch_array($q);
//$store=array();
//Compare
if($result[0][0] == $selected_choice){
//Answer is correct
$_SESSION['score']++;
}
//Check if last question
if($number == $total){
header("location: resultTable.php");
exit();
} else {
header("location: quizmain.php?n=".$next); //now the value of n is 2
}
}
?>
resultTable.php
<?php session_start(); ?>
<p>Final Score: <?php echo $_SESSION['score']; ?></p>
<?php
var_dump($_SESSION['RQuestionNumber']); //You will get all question number here
?>
<?php session_destroy(); ?>
You can store arrays on sessions, so every time you run process.php you can add an array with question number and given answer, that way at the resultTable.php you could just select the 10 questions from the db and compare with the given answers.
Add into process.php:
if(!isset($_SESSION['answers'])) {
$answers = new array();
} else {
$answers = $_SESSION['answers'];
}
$answers[] = array($_SESSION['RQuestionNumber'], $selected_choice);
$_SESSION['answers'] = $answers;
So what this code does is create an array to store question and user given answer and saves it in the session, this way you can loop this array on the resultTable and show all the question on the quiz.
PS: this is very basic code, there are a lot you need to do in order to use it on your quiz game, i.e. you should check if the array is not set from previous runs
You Can use Sessions for That
simple syntax of sessions is:
session_start();
$_SESSION['score'] = $data;
Now you can use your $data at another page by just starting session like this
session_start();
echo $_SESSION['score'];
To remove data you should destroy the session
session_destroy();
I have an issue where I need to loop the number of check boxes on a form submit. Foreach check box that is looped I need to then insert data into the database.
How Would I go about looping over the amount of check boxes that have being passed via form submit?
My code is as follows:
Form:
<form action="createChallenge.php" method="post" name="chalCreate">
Challenge Name:<input type="text" name="chalName" />
<br />
Challenge Target:<input type="text" name="chalTarget"/>
<br />
End Date:<input type="text" name="chalDate">
<br />
<!-- Needs a jquery datepicker -->
Select Friends: <br />
<?php
$selFriend = $conn->prepare("SELECT * FROM Friends WHERE UserID = '$userID' AND Friend = 'y' ORDER BY FriendName ASC");
$selFriend->execute();
foreach($selFriend as $row){
?>
<input type="checkbox" name="test" value="<?php echo $row['FriendID'] ?>"><?php echo $row['FriendName'] ?><br>
<?php
}
?>
<br />
<button type="submit">Create Challenge</button>
</form>
PHP to handle the form:
<?php
if(isset($_POST['test']))
{
$i = 0;
foreach($_POST['test'] as $checked)
{
echo $friend = $checked;
$i++;
}
echo $name = $_POST['chalName'];
echo $target = $_POST['chalTarget'];
echo $date = $_POST['chalDate'];
echo $friend = $_POST['test'];
echo $setby = $_COOKIE['userID'];
$create = $conn->prepare("INSERT INTO Challenge ( chalSetBy, chalName, chalTarget, chalDate ) VALUES ('$setby', '$name', '$target', '$date') ");
$create->execute();
if($create)
{
echo "Challenge made successfully";
}
else
{
echo "There was a problem";
}
}
?>
I thought doing the following would echo out data, but it didn't, it only selected the last check box:
$i = 0;
foreach($_POST['test'] as $checked)
{
echo $friend = $checked;
$i++;
}
Make an array of your checkbox in HTML page like as below,
<form name="frm" method="post">
<input type="checkbox" value="1" name="test[]">
<input type="checkbox" value="2" name="test[]">
<input type="checkbox" value="3" name="test[]">
<input type="checkbox" value="4" name="test[]">
<input type="checkbox" value="5" name="test[]">
<input type="checkbox" value="6" name="test[]">
<input type="submit">
</form>
<?php
foreach($_POST['test'] as $key=>$value)
{
echo $value."<br>";
}
I am getting the value for the single tsid for each record, however the checked radio button value is not returned in the array, I just get 0? Any help is appreciated.
PHP:
// Set the timesheets to set status approved/rejected
// find out how many records there are to update
$size = count($_POST['tsid']);
// start a loop in order to update each record
$i = 0;
while ($i < $size) {
// define each variable
$tsid = intval($_POST['tsid'][$i]);
$personnelid = intval($_POST['personnel'][$i]);
print "TSID: " . $tsid . "<br>";
print "TSuser: " . $personnelid . "<br>";
if ($tsid > 0 && $personnelid > 0) {
// do the update and print out some info just to provide some visual feedback
$query = "Update timesheets set status='1' where id= '$tsid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
}
++$i;
}
mysql_close();
HTML:
<input type="hidden" name="tsid[]" value="<?PHP echo $row['id']; ?>">
<li data-role="fieldcontain">
<a href="tsapprove.php?id=<?PHP echo $row['id']; ?>"><p><?PHP echo $row['personnel']; ?></p>
<p><?PHP echo $row['name']; ?></p>
<p class="ui-li-aside"><strong><?PHP echo $row['totalhrs']; ?> H</strong></p>
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="radio" name="personnel[]" id="1" value="<?PHP echo $row['personnel']; ?>" />
<label for="1">Approve</label>
<input type="radio" name="personnel[]" id="2" value="<?PHP echo $row['personnel']; ?>" />
<label for="2">Reject</label>
</fieldset>
</a>
View Details
</li>
Hopefully you can piece this together:
<?
$size = count($_POST['tsid']);
echo "<pre>";print_r($_POST);echo "</pre>";
$i = 0;
while ($i < $size) {
$tsid = $_POST['tsid'][$i];
$pid = $_POST['personnel_'.$i];
print "TSID: " . $tsid . "<br />";
print "TSuser: " . $pid . "<br />";
$i++;
}
?>
<form method="post">
<!--tsid[0] - $i = 0-->
<input type="hidden" name="tsid[]" value="1" />
<input type="radio" name="personnel_0" value="111" />
<input type="radio" name="personnel_0" value="222" />
<!--tsid[1] - $i = 1-->
<input type="hidden" name="tsid[]" value="2" />
<input type="radio" name="personnel_1" value="333" />
<input type="radio" name="personnel_1" value="444" />
<input type="submit" />
</form>
Basically, the way you are returning personnel doesnt really work. So I simplified this a bit, and basically set the radios for personnel differently. Instead of an array, its setting the actual name with the TSID value.
I hard coded 1 and 2, but you would replace with your $row['id'], as well has the number after "personnel_" would be 0..1..2 etc
Hopefully this helps
edit: few issues with code
I have the following html code:
<form method="post" action="arrayplay.php">
<input type="checkbox" value="1" name="todelete[]"/>
<input type="checkbox" value="2" name="todelete[]"/>
<input type="checkbox" value="3" name="todelete[]"/>
<input type="checkbox" value="4" name="todelete[]"/>
<input type="submit" value="delete" name="delete"/>
</form>
And the following PHP script:
//arrayplay.php
foreach ($_POST['todelete'] as $id)
{
echo $id . "<br/>";
}
?>
It is supposed to echo out each element value but instead I get an error. I am getting really frustrated. If I use:
<form method="post" action="arrayplay.php">
<?php
$dbc= //connection
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
echo $row['first_name'];
echo ' ' . $row['last_name'];
echo ' ' . $row['email'];
echo '<br />';
}
mysqli_close($dbc);
?>
<input type="submit" name="submit" value="Remove" />
</form>
It works perfectly fine! Why? The first (hard coded html) holds the exact same value as the one that retrieves them from the database. I am having a real hard time understanding retrieving values from an array with $_POST. Why does name=foo[] create an array? Is it an associative or numeric array? I'm sorry for all of the questions, I'm just really ready to pull my hair out.
if you just named the input foo it would only get one value. because square brackets are commonly used for arrays, foo[] is how in the html form, you indicate an array. of course on the PHP side you just call it foo as you are aware from your working example.
I've tested this and it should work:
<?php
if ($_POST['delete']) {
foreach ($_POST['todelete'] as $id) {
echo $id.' selected<br />';
}
}
?>
<form method="post" action="arrayplay.php">
<input type="checkbox" value="1" name="todelete[]"/>
<input type="checkbox" value="2" name="todelete[]"/>
<input type="checkbox" value="3" name="todelete[]"/>
<input type="checkbox" value="4" name="todelete[]"/>
<input type="submit" value="delete" name="delete"/>
</form>
If you're still having troubles, you can try:
<?php
if ($_POST['delete']) {
for ($i = 0; $i < 4; $i++) {
if (isset($_POST['todelete'][$i])) {
echo $_POST['todelete'][$i].' selected<br />';
}
}
}
?>