469,579 Members | 1,096 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Looping a MYSQL INSERT statement for a questionnaire tools answers

12
Can PHP help with the following as I have tried in the MYSQL Forums and cant get any help

Thanks

Nabz
----------------------------------------
Hi I am developing a PHP MYSQL questionnaire tool.

The problem I am having is that of inserting all the answers into the table. The questionnaire is Dynamic so the number of questions can vary. I have a variable that counts them so at the moment there is 75 questions.

Currently it will only insert the first value and return the the other 74 as having an empty value.

There are 2 bits of code im supplying, the first is the select statement which outputs the questionnaire and the radio buttons.

-- The counter represents the question number
-- The Question[ ] stores the question number
-- $row['q'] is the output for the question
-- item is the name for each answer
-- $row['qid'] is the question number and is attached to item to make each
answer unique, e.g.: - item1, item2, item3, item4

//Fetch and print the questionnaire.
$counter = 1;
while ($row = mysql_fetch_array($result,MYSQL_BOTH)) {
echo '<tr><td align="left">'.$counter. '</td><td align="left">'.$row['q'].'</td><td align="left" style="left">';
echo "<input type=\"hidden\" name=\"question[]\" value=\"".$row['qid']."\" />\n";
print("<input type=\"radio\" name=item".$row['qid'] ." value=\"1\" />1
<input type=\"radio\" name=item".$row['qid'] ." value=\"2\" />2
<input type=\"radio\" name=item".$row['qid'] ." value=\"3\" />3
<input type=\"radio\" name=item".$row['qid'] ." value=\"4\" />4
<input type=\"radio\" name=item".$row['qid'] ." value=\"5\" />5
<input type=\"radio\" name=item".$row['qid'] ." value=\"6\" />6
<input type=\"radio\" name=item".$row['qid'] ." value=\"7\" />7
<input type=\"radio\" name=item".$row['qid'] ." value=\"0\" />n/a");
'</td></tr>';
$counter++;
}


The Second bit of code is the insert statement that needs to be looped. It loops the insert statement but will only insert item1 and not all the way up to item75

//$answer[] = $_POST['answer'];
$answers = 1;

//if (isset($_POST['answer']) && is_array($_POST['answer']))
//{
//foreach ($_POST['answer'] as $answers) {
//}
if (isset($_POST['question']) && is_array($_POST['question']))
{
foreach($_POST['question'] as $question_no)
{
echo "Value: $answers<br>\n";
$query1 ="INSERT INTO answers (answer_score, question_id, user_id) ".
"VALUES ('".addslashes($answers)."', '".addslashes($question_no)."',$num11')";

$result1 = @mysql_query ($query1); // Run the query.

if (mysql_query($result1))
{
echo("Answers Submitted.<br>");
}
else
{
echo("The Answer did not submit: " . mysql_error());
}
}
}
}
}


I've tried putting them into arrays which then doesnt insert anything or doing a foreach staterment which also has the same undesired effect.

Any help would be much appreciated as I have been stuck on this for a while.

Thanks

Nabz
Mar 1 '07 #1
1 2829
ronverdonk
4,258 Expert 4TB
First read the Posting Guidelines at the top of this forum! With special regard to (a) double posting and (b) enclosing shown code within php or code tags.

The double thread is removed.

moderator
Mar 2 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by kaptain kernel | last post: by
4 posts views Thread by Chris | last post: by
reply views Thread by Mike Chirico | last post: by
2 posts views Thread by Delores | last post: by
22 posts views Thread by Andy Summers | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.