Jump to content

Retrieving the correct ID.


furrls

Recommended Posts

$qID = '';

$question = 'Question not set';

$answerA = 'unchecked';

$answerB = 'unchecked';

$answerC = 'unchecked';

$answerD = 'unchecked';

$answerE = 'unchecked';

 

 

 

?>

 

<html>

<head>

<script type="text/javascript">

function show_alert()

{

alert("Please Click OK to proceed!");

                 

}

</script>

</head>

<form action="Process1.php" method="POST">

<table>

<tr>

<td>

<?php

 

$SQL = "SELECT stu_satisfaction_tblquestions.question_id, stu_satisfaction_tblquestions.question, answer_type.answer1, answer_type.answer2, answer_type.answer3, answer_type.answer4, answer_type.answer5 FROM stu_satisfaction_tblquestions INNER JOIN answer_type ON stu_satisfaction_tblquestions.answers_id=answer_type.answers_id

";

$result = mysql_query($SQL);

while($db_field = mysql_fetch_assoc($result)){

 

$qID = $db_field['question_id'];

$question = $db_field['question'];

$A = $db_field['answer1'];

$B = $db_field['answer2'];

$C = $db_field['answer3'];

$D = $db_field['answer4'];

$E = $db_field['answer5'];

print $question;

?>

</td>

</tr>

<tr>

<td>

<INPUT TYPE = 'Radio' Name ='q'  value= 'A' <?PHP echo $answerA; ?>><?PHP echo $A; ?>

<INPUT TYPE = "Hidden" Name = "h"  VALUE = <?PHP print $qID; ?>>

<?php echo $qID;?>

</td>

</tr>

<tr>

<td>

<INPUT TYPE = 'Radio' Name ='q'  value= 'B' <?PHP echo $answerB; ?>><?PHP echo $B; ?>

<?php echo $qID;?>

</tr>

</td>

<tr>

<td>

<INPUT TYPE = 'Radio' Name ='q'  value= 'C' <?PHP echo $answerC; ?>><?PHP echo $C; ?>

</tr>

</td>

<tr>

<td>

<INPUT TYPE = 'Radio' Name ='q'  value= 'D' <?PHP echo $answerD; ?>><?PHP echo $D; ?>

</tr>

</td>

<tr>

<td>

<INPUT TYPE = 'Radio' Name ='q'  value= 'E' <?PHP echo $answerE; ?>><?PHP echo $E; ?>

<br>

<?php }  mysql_close(); ?>

</tr>

</td>

<tr>

<td>

 

 

Please add any comments you may have:</br>

<textarea rows="3" cols="60" name="comments" id="comments">

</textarea>

 

</td>

</tr>

 

 

</table>

 

<input type="submit" onclick="show_alert()" value="Submit" />

</form>

 

</html>

 

I ran to some problems here. After filling every question up with the answers. When i process the form, i only managed to get QID of 20 and the answer of 1st question as my database have only 20questions. How do i go about in solving, so that i'm able to retrieve all result and update my database correctly

 

$selected_radio = $_POST['q'];

 

 

$qID = $_POST['h'];

 

echo $selected_radio;

 

echo $qID;

 

$SQL = "UPDATE answers SET $selected_radio = $selected_radio + 1 WHERE question_id='$qID'";

$result = mysql_query($SQL);

mysql_close();

print "Thanks for voting!";

 

 

?>

THis is my process page, and i echo out the answer to be B and qid as 20.

Link to comment
Share on other sites

Here is a sample of how I would go about doing this.. Your current setup means that every question creates a hidden field with the name 'h' this will not work. You need a HTML array and iterate through it on post.

Example:

// Form Code //
$sql = "SELECT * FROM `q_and_a`";
$sql_query = mysql_query($sql);
while ($data = mysql_fetch_array($sql_query)) {
echo $data['question'].'<br/>';
echo '<input type="radio" name="answers['.$data['question_id']].'" value="A" />';
echo '<input type="radio" name="answers['.$data['question_id']].'" value="B" />';
echo '<input type="radio" name="answers['.$data['question_id']].'" value="C" />';
}

// Post Handler //
foreach ($_POST['answers'] as $qID=>$answer) {
echo 'The person answered '.$answer.' for qID '.$qID.'<br/>';
}

 

Hope this gives you something to work with.. It havent tested it but its something at least.

Also when posting in future, wrap your code in [ code ] or [ php ] tags :)

Link to comment
Share on other sites

hey, thanks for the codes. but i ran into some syntax errors

Parse error: syntax error, unexpected ']', expecting ',' or ';' in C:\xampp\htdocs\studsurvey.php on line 39

echo '<input type="radio" name="answers['.$data['question_id']].'" value="A" />';

Link to comment
Share on other sites

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\studsurvey.php on line 36

 

$SQL = "SELECT stu_satisfaction_tblquestions.question_id, stu_satisfaction_tblquestions.question, answer_type.answer1, answer_type.answer2, answer_type.answer3, answer_type.answer4, answer_type.answer5 FROM stu_satisfaction_tblquestions INNER JOIN answer_type ON stu_satisfaction_tblquestions.answers_id=answer_type.answers_id
";
$result = mysql_query($SQL);
while ($data = mysql_fetch_array($Sresult)) {

 

 

:(

Link to comment
Share on other sites

as for the post handler codes. Can i do this to update my database the results?

foreach ($_POST['answers'] as $qID=>$answer) {
echo 'The person answered '.$answer.' for qID '.$qID.'<br/>';
$SQL = "UPDATE answers SET $answers = $answers + 1 WHERE question_id='$qID'";
		$result = mysql_query($SQL);
		mysql_close();
		print "Thanks for voting!";
}

 

my fields in my database is current id,question_id, A, B, C, D, E

Link to comment
Share on other sites

ok thanks, if i wanna print out the options for each questions how do i do that? cos currently i have the radio button but didnt display the options or the answers. It is because there are different sets of answers such as - (Satisfied, not satifsied) or (agree,disagree)

should i change it to

echo '<input type="radio" name="answers['.$data['question_id'].']" value="<?php print $A" /><br/>';

?

Link to comment
Share on other sites

Something like this might work.

echo '<input type="radio" name="answers['.$data['question_id'].']" id="'.$data['question_id'].'_'.$A.'" value="'.$A.'" /><label for="'.$data['question_id'].'_'.$A.'">'.$A.'</label><br/>';

I added the ID attribute so you can write clickable labels for the radio buttons :)

Link to comment
Share on other sites

it didnt work, nothing came out. but for $answerA, the word "unchecked" appeared.

 

<?php
include 'config.php';


$qID = '';
$question = 'Question not set';
//$answerA = 'unchecked';
$answerB = 'unchecked';
$answerC = 'unchecked';
$answerD = 'unchecked';
$answerE = 'unchecked';




?>
<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("Please Click OK to proceed!");
                  
}
</script>
</head>
<form action="Process1.php" method="POST">
<table>
<tr>
<?php

$SQL = "SELECT stu_satisfaction_tblquestions.question_id, stu_satisfaction_tblquestions.question, answer_type.answer1, answer_type.answer2, answer_type.answer3, answer_type.answer4, answer_type.answer5 FROM stu_satisfaction_tblquestions INNER JOIN answer_type ON stu_satisfaction_tblquestions.answers_id=answer_type.answers_id
";
  
$result = mysql_query($SQL);
while ($data = mysql_fetch_array($result)) {
    $answerA = $db_field['answer1'];
$B = $db_field['answer2'];
$C = $db_field['answer3'];
$D = $db_field['answer4'];
$E = $db_field['answer5'];
echo $data['question'].'<br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" id="'.$data['question_id'].'_'.$answerA.'" value="'.$answerA.'" /><label for="'.$data['question_id'].'_'.$answerA.'">'.$answerA.'</label><br/>';
//echo '<input type="radio" name="answers['.$data['question_id'].']" value="A" /><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" value="B" /><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" value="C" /><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" value="D" /><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" value="E" /><br/>';
}
?>

Link to comment
Share on other sites

Where is the data for the names stored, the satisfied, unsatisfied stuff. It appears you are storing 'checked' or 'unchecked' in the $answerA variable..

 

Without a full scope of your system and what is stored where I cant really do much more :)

Link to comment
Share on other sites

<?php
include 'config.php';

$SQL = "SELECT stu_satisfaction_tblquestions.question_id, stu_satisfaction_tblquestions.question, answer_type.answer1, answer_type.answer2, answer_type.answer3, answer_type.answer4, answer_type.answer5 FROM stu_satisfaction_tblquestions INNER JOIN answer_type ON stu_satisfaction_tblquestions.answers_id=answer_type.answers_id
";
$result = mysql_query($SQL);
while($db_field = mysql_fetch_assoc($result)){


$A = $db_field['answer1'];
$answerB = 'unchecked';
$answerC = 'unchecked';
$answerD = 'unchecked';
$answerE = 'unchecked';


}


?>

<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("Please Click OK to proceed!");
                  
}
</script>
</head>
<form action="Process1.php" method="POST">
<table>
<tr>
<?php

$SQL = "SELECT stu_satisfaction_tblquestions.question_id, stu_satisfaction_tblquestions.question, answer_type.answer1, answer_type.answer2, answer_type.answer3, answer_type.answer4, answer_type.answer5 FROM stu_satisfaction_tblquestions INNER JOIN answer_type ON stu_satisfaction_tblquestions.answers_id=answer_type.answers_id
";
  
$result = mysql_query($SQL);
while ($data = mysql_fetch_array($result)) {

echo $data['question'].'<br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" id="'.$data['question_id'].'_'.$A.'" value="'.$A.'" /><label for="'.$data['question_id'].'_'.$A.'">'.$A.'</label><br/>';
//echo '<input type="radio" name="answers['.$data['question_id'].']" value="A"<br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" value="B" /><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" value="C" /><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" value="D" /><br/>';
echo '<input type="radio" name="answers['.$data['question_id'].']" value="E" /><br/>';
}
?>
<td>
</tr>
</td>
<tr>
<td>
Please add any comments you may have:</br>
<textarea rows="3" cols="60" name="comments" id="comments">
</textarea>

</td>
</tr>


</table>

<input type="submit" onclick="show_alert()" value="Submit" />
</form>

</html>

 

I managed to print out the 1st option, but it didnt tally with my database for the question and the set of answers, how do i go about doiing it ?

Link to comment
Share on other sites

BIG EDIT:

 

Removed everything I said before.

 

I have no idea, but when you are selecting lots of columns from the same table you can use *, so in your current statement it would become:

 

 

$SQL = "SELECT stu_satisfaction_tblquestions.question_id, stu_satisfaction_tblquestions.question, answer_type.* FROM stu_satisfaction_tblquestions INNER JOIN answer_type ON stu_satisfaction_tblquestions.answers_id=answer_type.answers_id";

 

One thing which tells me this wouldn't work is your use of answers_id. You shouldn't need to use that and definitely shouldn't have an answers_id in your questions table. You should be using a question_id in your answers table.

 

Like so:

 

ON stu_satisfaction_tblquestions.answers_id=answer_type.answers_id

 

becomes

 

ON stu_satisfaction_tblquestions.question_id=answer_type.question_id

 

If that doesn't work you need to seriously rethink how your are structuring your database. Ideally, each answer will be a new row. Not a row for 5 answers, it would be 5 rows for each answer, each with the same question_id.

Link to comment
Share on other sites

echo '<input type="radio" name="answers['.$data['question_id'].']" id="'.$data['question_id'].'_'.$A.'" value="'.$A.'" /><label for="'.$data['question_id'].'_'.$A.'">'.$A.'</label><br/>';

 

so nothing to do with this ? i suppose? just the query right ?

Link to comment
Share on other sites

read my post again:

 

"I have no idea".

 

The rest is a shot in the dark and a very serious suggestion for you to restructure how you are relating questions to answers. It's not right. How can a question have an answer id? The answer is a child of the question. The answer id shouldn't be in the questions table at all. The answers should have question id's and that is how the join works.

 

Maybe you need to do LEFT JOIN instead.

 

I'd restructure your database because if I started from scratch right now could have this running very smoothly within 20 minutes. Yet it would take that just to debug what you have. It's simple stuff so it isn't working already there is something fundamentally wrong.

 

Get your head clear, think about how question_id would work in both tables, with one answer for each row in the answers table. Create your select drop down again based on that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.