saving the options selected in quiz and checking - php

I have generated a quiz with questions on the same page.I want to save the answers to check with the answers in the database .How can I do it ? Below is the code for how I generated quiz.How to store the selected answer and check?
<?php for($i=1;$i<=3;$i++)//loop for extracting question and options
{?>
<form action = "CHECK.php" method="POST">
<?PHP
$sql = "SELECT QUESTION FROM T_QUESTIONS WHERE QUES_NO=$i LIMIT 1";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>QUESTION $i :</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['QUESTION'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
?>
<?PHP
$s = "select OPTION1,OPTION2,OPTION3,OPTION4,OPTION5,CORRECT_ANSWER from T_QUESTIONS where QUES_NO='$i'";
$result=mysqli_query($conn,$s);
if(!mysqli_query($conn,$s))
echo mysqli_error($conn);
else
while ($row = $result->fetch_assoc()) {
?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION1']; ?>" /> A. <?php echo $row['OPTION1']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION2']; ?>" /> B. <?php echo $row['OPTION2']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION3']; ?>" /> C. <?php echo $row['OPTION3']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION4']; ?>" /> D. <?php echo $row['OPTION4']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION5']; ?>" /> E. <?php echo $row['OPTION5']."<br>"; ?>
<br>
<?php
}
}
?>
</form>
<button style ="color:red;border radius:10px;margin:100px;height:100px;width:200px;font-size:30px"> <a href=CHECK.php>SUBMIT TEST</a></button>
Correct_OPTION is in the same table that of question and options.

Related

PHP and MySQL Radio Button Name = PHP ECHO

How can I get the clicked button value when its name is echoed by PHP?
Here's my code:
<form>
<input type='radio' name=<?php echo $i; ?> id='choice1' value=<?php echo $row['choices_a']; ?> checked/>
<?php echo '' .$row['choices_a']. '' ?>
<input type='radio' name=<?php echo $i; ?> id='choice2' value=<?php echo $row['choices_b']; ?> />
<?php echo ''.$row['choices_b'].''?>
<input type='radio' name=<?php echo $i; ?> id='choice3' value=<?php echo $row['choices_c']; ?> />
<?php echo ''.$row['choices_c'].''?> <br />
</form>
<?php
} echo $total;
?>
You can get value of that radio button is just simply like other inputs
$_POST['input_name']
And in your case if i = input_value,
$total = $_POST['input_value'];
echo $total;
You can Also check if that is clicked or isset
if(isset($_POST['input_value'])){
$total = $_POST['input_value'];
echo $total;
}
Hope it clears.

passing while loop value to another form

In the code bellow, I'm able to fetch values from the database. Now the retrieved values need to be passed to another form.
PHP:
<?php
require('administrator/connect-db.php');
$www_root = 'http://localhost/secure/cem/administrator/profile/';
$qry = mysql_query("SELECT * from dealer_package_details");
if(!$qry){echo mysql_error();}else{
while($row = mysql_fetch_array($qry)){
echo "<div class='col-sm-4 sm-margin-b-50'>";
echo "<form action='test.php'>";
echo "<div class='margin-b-20'>";
echo "<div class='wow zoomIn' data-wow-duration='.3' data-wow-delay='.1s'>";
echo '<img class="img-responsive" name="pport" src="', $www_root, '/', $row['pport'], '" alt="', $row['pport'], '"/>';
echo "</div>";
echo "</div>";
echo "<h3><a href='#' name='name_of_the_product'>" . $row['package_id'].$row['name_of_the_product']."</a></h3>";
echo "<input type='submit' name='submit' value='book now'>";
echo "</div>";
}
}
?>
The following code describes the second form where the values need to be passed.
HTML:
<form method="POST" action="insrt.php">
<input name="name_of_the_product" type="text" class="form-control name_of_the_product" value="<?php echo $_POST["name_of_the_product"]; ?>" readonly/>
<input name="package_id" type="text" class="form-control package_id" value="<?php echo $_POST["package_id"]; ?>" readonly/>
<input type="submit" name="submit" value="Save">
I suggest to use input hidden.
PHP:
...
echo "<input type='hidden' name='description' value='".$row['description]."' />";
...
HTML:
...
<input name="description" type="text" value="<?php echo $_POST['description']; ?>">
...

Edit sql data in php page

I need to edit my database table using submitted data.
This is the form:
mysql_query("set names 'utf8'");
$query = "SELECT * FROM sec1octa";
$result = mysql_query($query);
?>
<div align="center">
<form method="get" action="edit_data.php">
<table width="104" border="1" class="center1">
<tr>
<th width="94">first</th>
<th width="94">second</th>
<th width="94">status</th>
</tr>
<tr>
<?php
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><input type="text" name="id" value="<?php echo $row ['stu_no']; ?> " size=10></td>
<td><input type="text" name="name" value="<?php echo $row ['stu_name']; ?> " size=10></td>
<td><?php
echo '<select name="status">'; {
echo '<option value="open">'.$row['stu_status'].'</option>';
echo '<option value="close">'.prevent.'</option>';
}
echo '</select>';
?></td>
</tr>
<?php
}
}
?>
</tr>
</table>
<input type="submit" name="submit" value="done" />
</form>
The problem is in the edit_data.php page.
I can't UPDATE.
I use this code but it's not working.
require_once('../Connections/config.php');
$id= $_GET['id'];
$status= $_GET['status'];
$query= mysql_query("UPDATE `goh`.`sec1octa` SET `stu_status` = '$status'
WHERE stu_no='".$id."'") or die (mysql_error ());
if($query){echo $status ."done ";}
The reason you are only getting the last values in your edit_data.php $_GET is because you are not setting the input/select names as arrays.
<input type="text" name="id" value="some_stu_no">
is happening over and over and over and every new one overwrites the previous.
Instead, you should use:
<input type="text" name="id[]" value="some_stu_no">
This will allow you to pass multiple id's in a single form submission.
Your form:
<form method="POST" action="edit_data.php">
....
echo "<tr>";
echo "<th>id</th>";
echo "<th>name</th>";
echo "<th>status</th>";
echo "</tr>";
if(mysql_num_rows($result)>0){
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td><input type=\"text\" name=\"id[]\" value=\"{$row['stu_no']}\" size=\"10\"></td>";
echo "<td>{$row['stu_name']}</td>";
echo "<td>";
echo "<select name=\"status[]\">"; // I don't like your option set up here, but I don't fully understand it either.
echo "<option value=\"open\">{$row['stu_status']}</option>";
echo "<option value=\"close\">.prevent.</option>";
echo "</select>";
echo "</td>";
echo "</tr>";
}
}
....
<input type="submit" value="Submit All">
</form>
edit_data.php
// create a mysqli connection called $db
if(isset($_POST['id'])){
$tally=0;
// build all queries for the batch
foreach($_POST['id'] as $index=>$id){
$queries[]="UPDATE `goh`.`sec1octa` SET `stu_status`='".mysqli_real_escape_string($db,$_POST['status'][$index])."' WHERE `stu_no`='".mysqli_real_escape_string($db,$id)."'";
}
// run all queries
if(mysqli_multi_query($db,implode(';',$queries)){
do{
$tally+=mysqli_affected_rows($db);
} while(mysqli_more_results($db) && mysqli_next_result($db));
}
// assess the outcome
if($error_mess=mysqli_error($db)){
echo "Syntax Error: $error_mess";
}else{
echo "$tally row",($tally!=1?"s":"")," updated";
}
mysqli_close($con);
}

How to fetch POST values of a dynamically generated form in php

this is my dynamically generated form that allows the user to input scores of students.my challenge is how to capture the specific subjectCode and its score because am using the subjectcode to store in the database .
<?php
$attributes=array('class'=>'','id'=>'');
echo form_open('scoreInsertion/insertScores',$attributes);
echo "<table>";
echo "<tr>";
echo "<td>";?>
<label for="admNo>">Admission Number<span class="required">*</span></label>
<?php echo form_error('admNo');
echo "</td>";
echo "<td>";?>
<input type="text" name="admNo" id="admNo" value="<?php echo set_value('admNo')?>" />
<?php echo "</tr>";
echo "<tr>";
echo "<td>";
?>
<label for="studentName>">Student Name<span class="required">*</span></label>
<?php echo form_error('studentName');
echo "</td>";
echo "<td>";?>
<input type="text" name="studentName" id="studentName" value="<?php echo $studentName?>" />
<?php echo "</tr>";
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="examCategory">Exam Category<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="examCategory">
<option value="">--Select Category--</option>
<?php
print_r($exams);
if(isset($exams) && is_array($exams)){
foreach($exams as $exam){
$examId=$exam->id;
$examName=$exam->category;
echo "<option value='$examId'>$examName</option>";
}
}
?>
</select></td><?php
echo "<tr></tr>";
echo "<tr>";
echo "<td>";
?>
<label for="term">Academic Term<span class="required">*</span></label>
<!--<tr><td><?php echo form_error('examCategory')?></td></tr>-->
<td>
<select name="term">
<option value="">--Academic Term--</option>
<?php
if(isset($terms) && is_array($terms)){
foreach($terms as $term){
$termId=$term->termId;
$termName=$term->name;
echo "<option value='$termId'>$termName</option>";
}
}
?>
</select></td>
<?php
echo "<tr>";
//loop through the subjects array and display them on the view
if(isset($subjects) && is_array($subjects)){
foreach($subjects as $subject){
$subjectName=$subject->subName;
$subjectCode=$subject->subCode;
echo "<tr>";
echo "<tr></tr>";
echo "<td>";?>
<label for="<?php echo $subjectName?>"><?php echo $subjectName;?><span class="required">*</span></label>
<?php echo form_error($subjectName);
echo "</td>";
echo "<td>";?>
<input type="text" name="<?php echo $subjectCode?>" id="<?php echo $subjectName?>" value="<?php echo set_value($subjectCode)?>" />
<?php echo "</tr>";
}
}
?>
<?php echo "<tr>";echo "<td>";
echo form_submit('submit','Submit');
echo "</td>";
echo "</tr>";
?>
</p>
<?php
echo "</table>";
echo form_close();
?>
PHP script to process the above POST details is below:
function insertScores(){
$admNo=$this->input->post('admNo');
$term=$this->input->post('term');
//get the post variables from the Form
$posted = $this->input->post();
$x = array_keys($posted);
foreach($x as $y) {
echo $y ." = ". $_POST[$y]."<br/>";
echo "<br>";
//write the sql
$form_data=array(
'admNo' =>$admNo,
'subCode'=>$y,
'termId'=>$term,
'score'=>$_POST[$y]
);
print_r($form_data);
$this->SaveForm($form_data);
}
}
If that really has to be, you could set a specific prefix for the name attribute and then look for a key starting with that prefix among your $_POST keys. Something like that:
<input type="text"
name="subject_<?php echo $subjectCode?>"
id="<?php echo $subjectName?>"
value="<?php echo set_value($subjectCode)?>" />
Then search for it in the $_POST array.
$subjectCode = null;
foreach (array_keys($_POST) as $index) {
if (strpos($index, 'subject_') === 0) { // you found the needed element
// get its value and save it in a variable
$subjectCode = $_POST[$index];
break;
}
}
This code solved my problem, I fetch the subCode using admNo in the db then compare with the POST names.
$subjects=$this->subjectModel->get_subjects();
foreach($subjects as $subject){
$subCode= $subject->subCode;
foreach($_POST as $key => $value){
//print_r($key);
//print_r($value);
$form_data=array();
if($key == $subCode){
//echo $subCode. "=". $value;
//write the sql for insertion into the database
$form_data = array(
'admNo'=>$admNo,
'subCode'=>$subCode,
'termId'=>$term,
'formId'=>$formstudy,
'score'=>$value,
'year'=>$year
);
$this->SaveForm($form_data);
}
}
}

issues with stick forms in PHP

i'm trying to make this form sticky but it's not doing. What i'm trying to do is that from a previous form, the user enters some parameters and the HTML table is generated to this page, while another form is displayed to edit the data in the table shown above it. But the values are not displaying in the form below.
Please help me
PLEASE FORGIVE MY FORMATING I TYPED ON A MOBILE. Thanks for you patience.
<?php session_start(); ?>
<?php require 'includes/dbconnect.php' ; ?>
<?php require 'includes/header.inc.php'; ?>
<?php
$matric_no = mysql_real_escape_string($_POST['matric_no']);
if ($_POST['matric_no'] == "")
{
echo"<div id=\"contentRight\">";
echo"<idv id=\"msg\">" ;
echo "You didn't enter a <span style=\"color:red\">Matric Number</span>";
echo"</div>";
echo"</div>";
exit();
}
$query = "SELECT matric_no
FROM students
WHERE matric_no = '$_POST[matric_no]'";
$result = mysql_query($query);
$duplicates = mysql_num_rows($result);
if ($duplicates < 1)
{
echo"<div id=\"contentRight\">";
echo"<idv id=\"msg\">" ;
echo "You dont have a record for <span style=\"color:red\">$matric_no</span>" ;
echo "</div>" ;
echo "</div>" ;
exit();
}
$result = mysql_query("SELECT matric_no, first_name, last_name, other_name
FROM students
WHERE matric_no = '".mysql_real_escape_string($_POST['matric_no'])."'") or die(mysql_error());
$number_cols = mysql_num_fields($result) ;
echo "<div id=\"contentRight\">" ;
echo "<span class=\"header\">";
echo "<p><b>Matric Number: ".mysql_real_escape_string($_POST['matric_no']) ."</b></p>";
echo "<table border = 1 , cellspacing = 0 , cellpadding = 2 bgcolor=lemonchiffon >\n";
echo "<tr align=center>\n";
for ($i=0; $i<$number_cols; $i++)
{
echo "<th>" . mysql_field_name($result, $i). "</th>\n";
}
echo "</tr>\n";
while ($row = mysql_fetch_row($result))
{
echo "<tr align=center>\n";
for ($i=0; $i<$number_cols; $i++)
{
echo "<td>";
if (!isset($row[$i]))
{echo "NULL";}
else
{echo $row[$i];}
echo "</td>\n";
}
echo "</tr>\n";
}
echo "</table>";
echo"</span>" ;
echo"</div>";
?>
<br />
<?php
while($row = mysql_fetch_array($result)){
?>
<div id="contentRight">
<p>
Select the Score options you will like to update:
<form action="student_update2.php" method="post">
<ul>
<li>Matric Number: <input type="text" name="matric_no" value="<?php echo $row['matric_no'] ; ?>"> </li><br/><br/>
<li>First Name: <input type="text" name="first_name" value="<?php echo $row['firts_name'] ; ?>"> </li><br /><br/>
<li>Last Name: <input type="text" name="last_name" value="<?php echo $row['last_name'] ; ?>"> </li><br /><br />
<li>Other Names: <input type="text" name="other_name" value="<?php echo $row['other_name'] ; ?>"</li> <br /><br />
<input type="submit" name="submit" value="Update" />
<input type="hidden" name ="submitted" value="TRUE" />
<input name="Reset" type="reset" value="Reset" />
</ul>
</div>
</div>
</form>
<?php
}
require 'includes/footer.php'; ?>
formmatting cleaned a little
Try adding this:
$result = mysql_query("SELECT matric_no, first_name, last_name, other_name
FROM students
WHERE matric_no = '".mysql_real_escape_string($_POST['matric_no'])."'") or die(mysql_error());
Above this (approx line 99):
while ($row = mysql_fetch_array($result)){
?>
<div id="contentRight">
<p>Select the Score options you will like to update:</p>
I know you have this declared once already but since you've already use while to cycle through the recordset already it may need to be reset.

Categories