472,378 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 2939
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

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

Similar topics

8
by: kaptain kernel | last post by:
i've got a while loop thats iterating through a text file and pumping the contents into a database. the file is quite large (over 150mb). the looping causes my CPU load to race up to 100 per...
2
by: Venus | last post by:
Hi all, Newbie here!! I have 4 forms(A,B,C,D) and a thank you page for my questionnaire. People will be redirected to B or C depending on the last question's answer. B or C will redirect to D...
4
by: Chris | last post by:
Hi, sorry to post OT but i cant find the MySQL newsgroup, however i am hoping to pick up on some expert advice from php/mysql gurus here. I'm having some trouble performing a delete across two...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
2
by: Delores | last post by:
I have been running make-table queries to create 10 tables (one for each unit) that are exported to Excel spreadsheets. Because of my limited knowledge with code, I've created 10 separate queries...
2
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
2
by: javedna | last post by:
Hi Im doing a questionnaire in PHP and MYSQL I am trying to do a multiple insert using the following code $query1 = "INSERT INTO answers (answer_score,question_id,user_id) VALUES...
22
by: Andy Summers | last post by:
Hi All, Does anybody know how to determine C# skill set of a new hire? Our company is about to move to .Net and we have no experienced developers to assess C# skills . Are there any on-line...
3
by: Marjeta | last post by:
I have installed MySQL 5.0.67 yesterday. Previously we were using 4.0. On the development server, testing server, and training server this upgrade went smooth. The only thing I did there was run...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.