ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Coldfusion programming: How to submit muliple records at the same time that have the same field names.

Updated on October 14, 2012

 

Have you ever needed to have a web page, dispaly in a spreadsheet layout, with the same fields listed on multiple rows, with only one submit button.

Maybe, you have wanted to develop a survey questionnaire, which would have multiple questions with the same database field names, and only one submit button. Basically, your database would have fields, such as; QUESTION, ANSWER (just a simple example). The web page may have 10 questions on it, which would be 10 rows. You want the user to answer as many questions as applicable, and hit a single submit button.

You needed a way to loop through and insert each as it's own record, but with the same field names in the database. Also, you may have up to 10 rows of fields for the user to complete, but they may only answer 6 rows.

HTML example of form fields

In this example, we have a display of 4 rows with 3 columns, or 3 fields in the database and only one submit button. Because we are using the same field names, we will have to loop through the insert of the data into the database. We will need to make sure we are not just inserting blank records for the 4 rows of fields. I will use the EVALUATE function for checking each field when submitted.


The form property for the field and id will have basically a row number at the end of each, to work with the looping of the inserting of data. Let's say the 3 field names are: PRODUCTID, DESCRIPTION, QTY

Typically, you would see:
<cfinput type="text" name="productid" size="10" maxlength="10">

In this process, we will modify it, just a little by adding a row number to the name and id properties:
<cfinput type="text" name="productid1" id="productid1" size="10" maxlength="10">

So, you would end up with:
<cfinput type="text" name="productid1" id="productid1" size="10" maxlength="10">
<cfinput type="text" name="productid2" id="productid2" size="10" maxlength="10">
<cfinput type="text" name="productid3" id="productid3" size="10" maxlength="10">
<cfinput type="text" name="productid4" id="productid4" size="10" maxlength="10">

Use this same syntax with the other 3 fields.

<cfform action="?submitdata=yes" method="post">
<table border=0 cellpadding=0 cellspacing=2>
<tr>
<td valign=top align=center>PRODUCT ID</td>
<td valign=top align=center>DESCRIPTION</td>
<td valign=top align=center>QTY</td>
</tr>
<tr>
<td valign=top><cfinput type="text" name="productid1" id="productid1" size="10" maxlength="10"></td>
<td valign=top><cfinput type="text" name="description1" id="description1" size="40" maxlength="60"></td>
<td valign=top><cfinput type="text" name="qty1" id="qty1" size="2" maxlength="4"></td></tr>
<tr>
<td valign=top><cfinput type="text" name="productid2" id="productid2" size="10" maxlength="10"></td>
<td valign=top><cfinput type="text" name="description2" id="description2" size="40" maxlength="60"></td>
<td valign=top><cfinput type="text" name="qty2" id="qty2" size="2" maxlength="4"></td></tr>
<tr>
<td valign=top><cfinput type="text" name="productid3" id="productid3" size="10" maxlength="10"></td>
<td valign=top><cfinput type="text" name="description3" id="description3" size="40" maxlength="60"></td>
<td valign=top><cfinput type="text" name="qty3" id="qty3" size="2" maxlength="4"></td></tr>
<tr>
<td valign=top><cfinput type="text" name="productid4" id="productid4" size="10" maxlength="10"></td>
<td valign=top><cfinput type="text" name="description4" id="description4" size="40" maxlength="60"></td>
<td valign=top><cfinput type="text" name="qty4" id="qty4" size="2" maxlength="4"></td></tr>
<tr>
<td valign=top><cfinput type="text" name="productid5" id="productid5" size="10" maxlength="10"></td>
<td valign=top><cfinput type="text" name="description5" id="description5" size="40" maxlength="60"></td>
<td valign=top><cfinput type="text" name="qty5" id="qty5" size="2" maxlength="4"></td></tr>
<tr>
<td colspan=3 align=right><input type="submit" value="submit"></td></tr>
</table>
</cfform>

QUERY section

Here is the sample code for the actual insertion of the data into the database. Notice the line, <cfloopfrom="1"to ="4"index="i">
"4" is for the number of rows in the form.

<cfif isDefined('submitdata')>

<cfloop from="1" to ="4" index="i">


<cfquery name="insertrecs" datasource="yours"> 
         INSERT INTO ORDERS
		 (	productid,
		 	description,
			qty
		 	
			) 
         VALUES 
                  (    		
						<cfif isDefined("form.productid#i#") and Evaluate("form.productid#i#") NEQ ''>	
				  		#Evaluate("form.productid#i#")#,
						<cfelse>
						Null,
						</cfif>
						
						<cfif isDefined("form.description#i#") and Evaluate("form.description#i#") NEQ ''>	
				  		'#Evaluate("form.description#i#")#',
						<cfelse>
						Null,
						</cfif>
						
						<cfif isDefined("form.qty#i#") and Evaluate("form.qty#i#") NEQ ''>	
				  		'#Evaluate("form.qty#i#")#',
						<cfelse>
						Null,
						</cfif>
					 			
				) 
			  
   </cfquery> 
      
</cfloop>   

</cfif>

I hope the above example helps those looking for a way to insert multiple answers into the same field names in the database.

Check out some of the other Coldfusion articles below:

Comments

Submit a Comment

No comments yet.

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)