I'm trying to do a simple quiz using Codeigniter2.2.4 but I'm having some issues with checking the answers.
I have the following structure:
controllers: Guess
models: People
views: guessview, results
in my people class I have a methods to get all questions from the database and to get a random one. I'm having issues checking if the answer is right or no. It always comes back false
function getQuestions()
{
if(!isset($questions))
{
$this->db->select("id, name, choice1, choice2, correctAnswer");
$this->db->from('questions');
$query = $this->db->get();
foreach ($query->result() as $row) {
$questions[] = $row;
}
}
return $questions;
}
function getRandomQuestion()
{
$max = count($this->getQuestions());
$randpos = rand(0, $max-1);
$question = $this->getQuestions()[$randpos];
return $question;
}
And in my controller :
function guess()
{
$answer = $this->input->get('answers',false);
//$questionId = $this->input->get('id',false);
//this will generate random questions.
$newQuestion = $this->people->getRandomQuestion();
$this->load->view('guessview',$newQuestion);
//this is always false
if($answer == $newQuestion->correctAnswer)
{
//do something
}
}
And in my view I have only a form displaying only one question at a time.
//Update
When using the function you provided I'm getting some errors in my controller and view. I try grabbing a question in my controller
$question = $this->people->getRandomQuestion();
//this give me undefined index correctAnswer but should it not have since the database table has the following structure : id, name, choice1, choice2, correctAnswer ?
if($question['correctAnswer'] == $answer)
{
}
Also when I want to pass data to the view i.e
$question = $this->people->getRandomQuestion();
$data['question'] = $question;
$this->load->view('guessview',$data);
And in my view for the form values I tried like this: $question['name'], $question['id'] and I get undefined index name,id etc.
My form from guessview.php
<form id= "form1" class="form" action="<?php echo base_url();?>guess/people" method='POST'>
<input type=hidden name=id value="<?php echo $question['id'] ?>">
<label><?php echo $question['name'] ?>
<div class="radio">
<label>
<input type="radio" id='rad1' name="answers" value="<?php echo $question['choice1'] ?>">
<?php echo $question['choice1'] ?>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="answers" value="<?php echo $question['choice2'] ?>">
<?php echo $question['choice2'] ?>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="answers" value="<?php echo $question['correctAnswer'] ?>">
<?php echo $question['correctAnswer'] ?>
</label>
<div>
<input type=submit name="sub" value="Answer">
</div>
</form>
The problem why I posted was regarded to the controller function. It seems that when I try to check if the user answered correctly is always false. Like what the user answered the previous question get compared with the correctAnswer of the current question.
I tried getting the input of the radio button and id(put in a hidden field)
if these were returning false it meant that no questions are display and displayed first question. else if there was input then get the question based on the id and compare the question's correctAnswer to what the user selected.(But like I said above it would always be false). I will post the method when I get home.
You can do it in single Model function
function getRandomQuestion()
{
$sql = "SELECT * FROM questions ORDER BY RAND() LIMIT 1 )";
$query = $this->db->query($sql);
$result = $query->result_array();
$count = count($result);
if (empty($count) ||$count > 1) {
echo "Inavliad Question";
}
else{
return $result;
}
}
Related
I have a table in the database, I can edit a column of particular row at a time. but, i want to edit multiple columns of a single row at a time with an option of edit and then save it to the database ,we can do it from back-end by editing in the database itself. But, i want to do it from front-end .Now i am able to display the data in the table. I am new to coding , so can anyone help me where to start for this particular task of editing multiple columns at the same time.
Here is the code for displaying the data..
<?php
include('db.php');
include 'header.php';
include 'gobacktomenu.php';
$TND_ID = $_GET['TND_ID'];
$sql = "SELECT * FROM new_tnds WHERE TND_ID = ('".$TND_ID."')";
$result = $link->query($sql);
echo "<div style='overflow-x:auto;'><table border = '1'><font size = '2' face='verdana'>
<th>TND ID</th><th>Site name S1</th><th>Site name S2</th><th>Call sign S1</th><th>Idea ID S1</th><th>Call sign S2</th><th>Idea ID S2</th><th>Site to integrate</th><th>True azimuth (°) S1</th><th>True azimuth (°) S2</th><th>Pathlength (km)</th><th>TR Antenna diameter (m)</th><th>TR Antenna height (m) S1</th><th>TR Antenna height (m) S2</th><th>Channel ID S1</th><th>Channel ID S2</th><th>Design frequency S1</th><th>Design frequency S2</th><th>Polarization</th><th>Radio model</th><th>TX power (dBm)</th><th>Receive signal (dBm)</th><th>Planning Remarks</th><th>Projects Remarks Uploaded by</th><th>O & M Remarks Uploaded by</th><th>Planning Remarks Uploaded by</th><th>TXN NOC Remarks Uploaded by</th></tr>";
if ($result->num_rows > 0) {
// output data of each row
$row = $result->fetch_assoc();
echo "<tr><td>".$row["TND_ID"]."</td><td>".$row["Site_name_S1"]."</td><td>".$row["Site_name_S2"]."</td><td>".$row["Call_sign_S1"]."</td><td>".$row["Idea_ID_S1"]."</td><td>".$row["Call_sign_S2"]."</td><td>".$row["Idea_ID_S2"]."</td><td>".$row["Site_to_integrate"]."</td><td>".$row["True_azimuth_S1"]."</td><td>".$row["True_azimuth_S2"]."</td><td>".$row["Path_length_(km)"]."</td><td>".$row["TR_Antenna_diameter_(m)_S1"]."</td><td>".$row["TR_Antenna_height_(m)_S1"]."</td><td>".$row["TR_Antenna_height_(m)_S2"]."</td><td>".$row["#1_Channel_ID_S1"]."</td><td>".$row["#1_Channel_ID_S2"]."</td><td>".$row["#1_Design_frequency_S1"]."</td><td>".$row["#1_Design_frequency_S2"]."</td><td>".$row["Polarization"]."</td><td>".$row["Radio_model_S1"]."</td><td>".$row["TX_power_(dBm)_S1"]."</td><td>".$row["Receive_signal_(dBm)_S1"]."</td><td>".$row["Planning_Remarks"]."</td><td>".$row["Projects_remarksupdated_user_name"]."</td><td>".$row["O_M_remarksupdated_user_name"]."</td><td>".$row["Planning_remarksupdated_user_name"]."</td><td>".$row["txn_noc_remarksupdated_user_name"]."</td></tr>";
}
echo "</table></div>";
$link->close();
?>
you can use this structure ,this can be used for editing 2 tables and multiple columns table a has columns (aid,a1,a2) table b has columns (bid,b1,b2) i know code looks little rough but just wrote it in 5 min to give you an idea
<?php
//this only executes when button is clicked
if (isset($_POST["submit"] && $_POST["submit"]!=""){
$counta=$_POST["aid"];
//loops through the posts inside here do your query with the set command
//like "update a set a1='$a1',a2='$a2" where aid ='$_POST["aid'][$i]"
for($i=0;$i<$count;$i++){
$a1[$i]=$_POST["a1"][$i];
$a2[$i]=$_POST["a2"][$i];
//your query here
}
$countb=$_POST["bid"];
//loops through the posts inside here do your query with the set command
//like "update b set b1='$b1',b2='$b2" where aid ='$_POST["bid'][$i]"
for($i=0;$i<$count;$i++){
$a1[$i]=$_POST["a1"][$i];
$a2[$i]=$_POST["a2"][$i];
}
//now the part before the button gets clicked
//do your query to select data from database
?>
<html>
<body>
//creating loop inside html
<?php
$a=0;
while($rowa = mysql_fetch_assoc($resulta){
?>
//getting the input data
<input type="checkbox" name="aid[]" value="<?php echo $row["aid"]; ?>>
<label>a1</label>
<input type="text" name="a1[]" value="<?php echo $row["a1"]; ?>>
<label>a2</label>
<input type="text" name="a2[]" value="<?php echo $row["a2"]; ?>>
//ending the loop
<?php
$a++;
}
//b works the same as a
$b=0;
while($rowb = mysql_fetch_assoc($resultb){
?>
<input type="checkbox" name="bid[]" value="<?php echo $row["bid"]; ?>>
<label>b1</label>
<input type="text" name="b1[]" value="<?php echo $row["b1"]; ?>>
<label>b2</label>
<input type="text" name="b2[]" value="<?php echo $row["b2"]; ?>>
<?php
$b++;
}
?>
<input type="submit" name="submit" value="submit">
</body>
</html>
You need use form instead of table while editing and pass the fetched values on input of form and write update query on form submit.
if ($result->num_rows > 0) {
// output data of each row
$row = $result->fetch_assoc();
}
if(isset($_POST['submit']))
{
$query = "update details set Site_name_S1='$_POST['submit']',Site_name_S2='$_POST['Site_name_S2']',Call_sign_S1='$_POST['Call_sign_S1']',Idea_ID_S1='$_POST['Idea_ID_S1'] where TND_ID=$_POST['TND_ID']";
$res = mysql_query($query);
}
<form method="post">
<input type="text" name="id" value="<?php echo $row['TND_ID'] ;?>" >
<input type="text" name="Site_name_S1" value="<?php echo $row['Site_name_S1'] ;?>" >
<input type="text" name="Site_name_S2" value="<?php echo $row['Site_name_S2'] ;?>" >
<input type="text" name="Call_sign_S1" value="<?php echo $row['Call_sign_S1'] ;?>" >
<input type="text" name="Idea_ID_S1" value="<?php echo $row['Idea_ID_S1'] ;?>" >
<input type="submit" name="submit">
</form>
Just check the quotes while writing update query.
You can use Ajax
Just a fragment of code:
.............
<td><input type="text" row_id=".$some_row_num_from_table."
col-name=".$some_col_name_from_table." disabled class="some-val" value=".$some_from_base."></td>
.............
than little Ajax
$('.some-val').dblclick(function() {
$(this).attr('disabled', false);
});
$('.some-val').blur(function() {
$(this).attr('disabled', true);
var send_data = { 'row_id':$(this).attr('row_id'),
'col-name':$(this).attr('col-name'),
'myval':$(this).val()
};
$.ajax({
url:'your_url.php',
data:send_data,
method: 'post',
success: function(data){...}
});
});
your_url.php
if(isset($_POST['row_id'])){
$sql = "UPDATE mytablename SET `".$_POST['col-name']."` = ".$_POST['myval']." WHERE `row_id` = ".$_POST['row_id'];
$link->query($sql); // it s bad query without protect of sql injection JUST DEMO
}
My URL currently has:
/add_products_flavour.php?pro_id=2
in it. However, the pro_id value gets lost after I submit my form. I want to keep returning to the same page and keep the value of pro_id to be able to submit the form multiple times.
The form:
<form name="flavour_form" method="post" action="add_products_flavour.php">
<label class="add_products_field_label" for="flavour">Aroma:</label>
<input class="w-input" data-name="flavour" id="flavour" maxlength="256" name="flavour" required="required" type="text">
<input class="add_products_form_button w-button" type="submit" name="insert_post" value="Adauga">
<?php insertFlavour()?>
</form>
function insertFlavour(){
global $connection;
$pro_id=getProductID();
if (isset($_POST['insert_post'])) {
$flavour = $_POST['flavour'];
$insert_flavour_query = "insert into flavour (name) values ('$flavour')";
$run_insert_flavour = mysqli_query($connection, $insert_flavour_query);
if($run_insert_flavour){
echo"<script>alert('flavour added!')</script>";
echo "<script>window.open('add_products_flavour.php?pro_id=$pro_id','_self')</script>";
}
}
}
function getProductID(){
if(isset($_GET['pro_id'])) {
$pro_id = $_GET['pro_id'];
return $pro_id;
}
}
solved it by doing
<?php $pro_id = $_GET['pro_id'];
echo "<form name='flavour_form' method='post' action='add_products_flavour.php?pro_id=$pro_id'>";
?>
I am currently doing up a list of names that has signed up for a activity which is then needed to go through another selection process and to be put into the database. I have currently done the getting and displaying of names from the people that has signed up.
However, I am not too sure on the process of how I can get those checked which is being selected in the selection process.
This is currently the codes that I have done to get and display the names of the people who have signed up. Upon clicking submit, it would go to another page which would then process, get those checked and marked as 1 or true to the database (MySQL) or either set as 0 or false if it has not been checked.
<?php
if ($totalShortlist > 0) {
?>
<?php
while ($row = mysqli_fetch_assoc($result)) {
$firstName = $row['first_name'];
$lastName = $row['last_name'];
?>
<form id="selectionProcess" action = "doShortlistProcess.php?id=<?php echo $id ?>" method = "post">
<fieldset data-role="controlgroup">
<label><input type="checkbox" name="selectionProcess" id="selectionProcess" value="<?php echo $id ?>"/><?php echo $firstName; ?> <?php echo $lastName; ?></label>
</fieldset>
<?php
}
?>
<input type="submit" class="btnSelect" data-theme="b" data-inline="true" name="shortlistCandidate" value="Submit Shortlisted Candidates"/>
</form>
<?php
}
// end for loop
else {
echo "No candidates to be shortlisted";
}
mysqli_close($link);
?>
The $id as shown is the brought over from the list of categories on the previous page, which allows to get all the names in that categories to be displayed.
Your code places the form in the wrong place compared to the while loop. The action also doesn't need you to compose a query string. Better code (careful, I've also cut out some presentation):
if ($totalShortlist > 0) {
?>
<form id="selectionProcess" action = "doShortlistProcess.php" method = "post">
<fieldset>
<?php
// use while loop to display a tick box for each item
while ($row = mysqli_fetch_assoc($result)) {
$firstName = $row['first_name'];
$lastName = $row['last_name'];
?>
<label>
<input type="checkbox" name="selectionProcess" id="selectionProcess" value="<?php echo $id ?>"/>
<?php echo $firstName; ?> <?php echo $lastName; ?>
</label>
<?php
} // end of while loop
?>
</fieldset>
<input type="submit"/>
</form>
<?php
} // end of shortlist > 0
I recommend you test this code using the GET method. It will show you what form the submission takes.
To process the result, you should use the list of responses, process it and turn it into one or more SQL statements.
An alternative would be to use an action for each checkbox, but then use ajax to a php page that would modify the status of single subscriber. The change would be made immediately as users click.
I have a page that is created dynamically from the information that a user has submitted on a previous page. For example on page 1, the user inputs some department names. They can enter as many as they want. On page 2, multiple sections are created for each department that was entered on page one. Inside each of these sections are going to be forms, I currently have it set up so that the names of the forms are created using a variable $a. I need to know how to post these items once the submit button in each section is clicked. I have tried several different ways and nothing is working. I want it so that ONLY the items with the same $a value as the submit button's $a get posted.
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a;?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a;?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
<?php
echo "</div>";
}
?>
*EDIT I need everything to be in one form because the point of the page is to add up all of the values that will be inserted into each of the individual sections later. *
**EDIT 2 :
I figured it out using #dirt 's jquery suggestion.
HTML:
$departmentSql = "SELECT * FROM b_departments WHERE loc_id='$locid' AND b_id = '$bid'";
$departmentResults = mysql_query($departmentSql,$con);
$a = 0;
while ($departmentRow = mysql_fetch_array($departmentResults)) {
$department = $departmentRow['b_dep_name'];
$departmentID = $departmentRow['dep_id'];
$b_departmentID = $departmentRow['b_dep_id'];
$a++;
echo "
<div id=depDiv>
<div id=depDivHeader>
".$department."
</div>
<div id=$a>
";
$areaSql = "SELECT * from areas WHERE dep_id = $departmentID ORDER BY area_name ASC";
$areaSqlResult = mysql_query($areaSql);
?>
<br />
<input type="hidden" name="bdep<?php echo $a ?>" value="<?php echo $b_departmentID; ?>" />
Add Area:
<select name="dep_area<?php echo $a ?>" type="menu">
<option value=""></option>
<?php while($areaRows=mysql_fetch_assoc($areaSqlResult)){?>
<option value="<?php echo "".$areaRows['area_id'].",".$areaRows['area_name']."" ?>"><? php echo $areaRows[ 'area_name'] ?></option>
<?php }
?>
</select>
<button type="submit" name="areaSub" value="<?php echo $a ?>" />Add</button>
<?php
echo "</div></div>";
} ?>
jQuery:
<script>
$(document).ready(function() {
$('#<?php echo $a ?>').submit(function() {
.post("include/action.php")
});
});
</script>
PHP:
if(isset($_POST['areaSub'])) {
$areaval = intval($_POST['areaSub']);
$area = mysql_real_escape_string($_POST["dep_area".$areaval.""]);
list($area_id, $area_name) = explode(',', $area, 2);
$bdep = mysql_real_escape_string($_POST["bdep".$areaval.""]);
echo $areaval;
echo $area_name;
echo $bdep;
}
EDIT: If its a single form with multiple sections and you don't want to trim out unwanted form data after the POST then I think you must use jQuery to trim the form values prior to the post to the server, so see my jQuery portion below for a crued example of how you would go about only posting the data for the selected button.
Short answer would be to use the Name/Value attributes of the submit button and evaluate that once posted. Multiple buttons with the same name can have different values and the values don't have to be the labels.
Example:
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<form id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<button type='submit' name='submit' value='<?php echo $a; ?>'>Add</button>
</form>
...
<?php
# _POST:
if (isset($_POST['submit']) && $_POST['submit'] == '1') {
echo 'Department 1';
}
if (isset($_POST['submit']) && $_POST['submit'] == '2') {
echo 'Department 2';
}
?>
You could also use jQuery to get all elements contained in a certain Div ID. Something like (this is rough idea):
<div id='<?php echo $a; ?>'>
<input type='text' name='in1'>
<input type='text' name='in2'>
<input type="submit" name="areaSub<?php echo $a;?>" value="Add" />
</div>
jQuery:
$('#<?php echo $a; ?>').submit(function() {
do_something_with_form_$a
});
I'm not sure if I understand completely... but the issue you're having is that, when a user hits the 'submit' button for a given set of values (a given department), it submits ALL of the data on the page? And you only want the input fields for that specific area submitted?
Section off forms with the tag:
<form> </form>
So, you'll have multiple areas:
while(...) {
<form method="..." name="form $a">
<input name="... $a">
<input name="... $a">
...
<submit>
</form>
}
(Obviously this is pseudocode, adjust accordingly)
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Simplest method of passing one php variable in a url
I am making a quiz application. At the faculty end, teachers get to type the topic name (with which I will form a vanity URL), and type 3 questions with possible options including answer in one of them. Everytime a teacher creates a quiz, a new table will be created with the questions, options and answers everytime.
My db.php file:
<?php
mysql_connect("localhost","root","");
mysql_select_db("quiz");
?>
My faculty.php file:
<form action="fac_handler.php" method="POST">
Quiz Topic:<br><input type="text" name="topic"><br><br>Enter Questions:<br><br>
<?php
$a="a";
$b="b";
$c="c";
$d="d";
for ($i=1; $i<=3;$i++)
{
?>
Question <?php echo $i?>:<br>
<textarea type="text" name="<?php echo "q".$i;?>"></textarea><br>
<br>
<input type="text" name="<?php echo $a.$i; ?>">
<input type="text" name="<?php echo $b.$i; ?>">
<input type="text" name="<?php echo $c.$i; ?>">
<input type="text" name="<?php echo $d.$i; ?>"> <br><hr>
<?php
}
?>
<br>
<input type="submit" value="Submit">
</form>
My fac_handler.php file:
<?php
require "db.php";
if(isset($_POST['topic']) && !empty($_POST['topic']))
{
$topic=$_POST['topic'];
if(isset($_POST['q1'])&&isset($_POST['a1'])&&isset($_POST['b1'])&&isset($_POST['c1'])&&isset($_POST['d1']))
{
if(!empty($_POST['q1']) && !empty($_POST['a1']) && !empty($_POST['b1']) && !empty($_POST['c1']) && !empty($_POST['d1']))
{
$q1=$_POST['q1'];
$a1a=$_POST['a1'];
$a1b=$_POST['b1'];
$a1c=$_POST['c1'];
$a1d=$_POST['d1'];
}
else
{
echo "Please Set the Questions and the Options!";
die();
}
}
if(isset($_POST['q2'])&&isset($_POST['a2'])&&isset($_POST['b2'])&&isset($_POST['c2'])&&isset($_POST['d2']))
{
if(!empty($_POST['q2']) && !empty($_POST['a2']) && !empty($_POST['b2']) && !empty($_POST['c2']) && !empty($_POST['d2']))
{
$q2=$_POST['q2'];
$a2a=$_POST['a2'];
$a2b=$_POST['b2'];
$a2c=$_POST['c2'];
$a2d=$_POST['d2'];
}
else
{
echo "Please Set the Questions and the Options!";
die();
}
}
if(isset($_POST['q3'])&&isset($_POST['a3'])&&isset($_POST['b3'])&&isset($_POST['c3'])&&isset($_POST['d3']))
{
if(!empty($_POST['q3']) && !empty($_POST['a3']) && !empty($_POST['b3']) && !empty($_POST['c3']) && !empty($_POST['d3']))
{
$q3=$_POST['q3'];
$a3a=$_POST['a3'];
$a3b=$_POST['b3'];
$a3c=$_POST['c3'];
$a3d=$_POST['d3'];
}
else
{
echo "Please Set the Questions and the Options!";
die();
}
}
$ans1=$a1a;
$ans2=$a2a;
$ans3=$a3a;
$qtbname="q_".$topic;
$top=$_SESSION['topic']=serialize($_POST['topic']);
$new="q_".$top;
$create_table="CREATE TABLE $qtbname(id int NOT NULL AUTO_INCREMENT,question VARCHAR(500),answer VARCHAR(30),optiona VARCHAR(30),optionb VARCHAR(30),optionc VARCHAR(30),optiond VARCHAR(30),PRIMARY KEY (id))";
mysql_query($create_table);
$query1="INSERT INTO $qtbname VALUES('','$q1','$ans1','$a1a','$a1b','$a1c','$a1d')";
$query2="INSERT INTO $qtbname VALUES('','$q2','$ans2','$a2a','$a2b','$a2c','$a2d')";
$query3="INSERT INTO $qtbname VALUES('','$q3','$ans3','$a3a','$a3b','$a3c','$a3d')";
$result1=mysql_query($query1);
$result2=mysql_query($query2);
$result3=mysql_query($query3);
header("Location: student.php");
}
else
{
echo "Please Set the Quiz Topic";
}
?>
Now as you can see, after the table gets created dynamically everytime with a table prefix, it gets redirected to student.php file which looks like this.
My student.php file:
<?php
require "db.php";
$query="SELECT * FROM $new";
$result=mysql_query($query);
$n="1";
$i="a";
while($row=mysql_fetch_assoc($result))
{
echo "Question ".$n." : ".$row['question'];
echo "<br>";
$ansr=$row['answer'];
?>
<form action="std_handler.php" method="POST">
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optiona']; ?>"><?php echo $row['optiona']; ?>
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optionb']; ?>"><?php echo $row['optionb']; ?>
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optionc']; ?>"><?php echo $row['optionc']; ?>
<input type="radio" name="<?php echo $i; ?>" value="<?php echo $row['optiond']; ?>"><?php echo $row['optiond']; ?><br>
<?php
++$i;
++$n;
}
?>
<input type="submit" value="Submit">
</form>
The fourth line in this student.php file, the query, $query="SELECT * FROM $new";, I need to access the table which I just created at the faculty end to display the questions and answers in text and radio button format.
Here is my std_handler.php file:
<?php
if(isset($_POST['a']))
{
$ans=$_POST['a'];
echo $ans;
echo "<br>";
}
if(isset($_POST['b']))
{
$ans=$_POST['b'];
echo $ans;
echo "<br>";
}
if(isset($_POST['c']))
{
$ans=$_POST['c'];
echo $ans;
echo "<br>";
}
?>
In the faculty handler file, after the validations, I tried to use session and get the variable, I tried to make it global, I tried serializing it, I couldn't succeed to use and select the dynamic table name in the student.php page.
What I want is, as soon as I get redirected to the student.php page, I want to select the table instantly created earlier, how do I do that? How do I access that variable with which I created the table and executed the query at the faculty end?
I keep getting this error: Notice: Undefined variable: new in C:\xampp\htdocs\quiz\student.php on line 4
Please help me solve this problem. Thank you.
Do you want something like this:
Bad solution, but you can see how to pass a variable
In fac_handler.php:
Change
header("Location: student.php");
to
header("Location: student.php?url=". urlencode($new));
On the student page:
$query="SELECT * FROM " . mysql_real_escape_string($_GET["url"]);
This is a method, but if I where you I would do this on an other way.
How I would do this
Create a ID that you send and resolve that name to the ride database name.
If I had to guess I would say this. When it gives you this error: Notice: Undefined variable: new in C:\xampp\htdocs\quiz\student.php on line 4 it's because it's just that. If we look at the code here:
require "db.php";
$query="SELECT * FROM $new";
$result=mysql_query($query);
$n="1";
$i="a";
this is your student.php ^^^
You are not setting the value of $new.
So you need to actually define the value.
$new = $_GET['x'];
$query="SELECT * FROM $new";
$result=mysql_query($query);
$n="1";
$i="a";
Where x is whatever you are passing as your query value.
or something along those lines.
Okay, I solved this problem, this is what I did.
I removed these two from the fac_handler.php page:
$top=$_SESSION['topic']=serialize($_POST['topic']);
$new="q_".$top;
And few lined below, I changed the header location to:
header("Location: student.php?url=".$qtbname);
And in the student.php page, I changed my query to:
$query="SELECT * FROM " . mysql_real_escape_string($_GET["url"]);
For the last two modifications above, I sincerely thank Lawrence. Also Thunda thanks a lot for giving me that idea to use the GET variable thing too.
deceze and tim.baker (there in the comments): Thanks a lot for your advice too, now that I solved my problem, I will go ahead with normalization as you suggested.
Thanks a lot stackoverflow, was struggling with this for a long time.