473,769 Members | 8,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping a MYSQL INSERT statement for a questionnaire tools answers

12 New Member
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_arr ay($result,MYSQ L_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($r esult1))
{
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 3035
ronverdonk
4,258 Recognized Expert Specialist
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
4061
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 cent. Even if i remove the mysql insert query and just loop through the file , it still hits 100 per cent CPU. This has the knock on effect of slowing my script down so that mysql inserts are occuring every 1/2 second or so.
2
2433
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 after B or C is finished. D then goes to the thank you page. I want to get all the answers from A,B/C,D first and then insert all
4
5307
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 tables. The tables i have are: questionnaires (id, name); questionnaire_questions (questionnaires_id, id, name, qf_type)
0
3948
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 version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
2
1584
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 to do the job and then run a macro to do the exporting. I know there must be a way to generate the 10 tables with only one query and I think it probably involves looping(?) through the query. I've read the previous postings and anytime I try to...
2
4722
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 question. I have a web survey that I am working on and have successfully dynamically taken the info from a database, displayed it on the screen and then taken the users answers and inserted them into a
2
4048
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 ('$item1','$question','$user_id_foreign'), ('$item2','$question','$user_id_foreign')"; But it will only insert the first value and not the second. I have tested the mysql bit of coding in mysql itself with some values I manuall input i.e.: - (4, 1, 1), (3,...
22
4559
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 questionnaires you can point us to? Thanks, Andy
3
6357
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 mysql_fix_privilege_tables. But yesterday, after doing this same thing on production server, something went wrong, at least with one database. While I was in the office things looked fine. But this morning I noticed error messages like: 090119...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.