Skip to main content
Inspiring
October 15, 2025
Answered

Use input fields in a loop to update existing values in a database

  • October 15, 2025
  • 1 reply
  • 153 views

Cannot use the data entry field on the page (this must be so basic, but I cannot get it to work)

In the extract below NEWVALUE field is not recognised

 

Process :

The user selects a case and is taken to another page that displays a list of entries linked to a case (so the number varies each time).

They complete an input field to update one or more the entry values.

They then press the submit button and the page refreshes with the new values.

 

extract :

<cfquery name="table1"...> SELECT ...</cfquery>

 

<cfoutput>

<input type="submit">

<cfform action="page2.cfm"  method="POST" name="FormX">                 

    <table width="200" border="1" cellspacing="0" cellpadding="2">

 

       <cfloop query="table1">

         <tr>

             <td nowrap="nowrap">#field#</td> ....  <!--- display fields --->

             <td>New value</td><td><cfinput type="text" name="NEWVALUE" maxlength="20" size="20"></td>   <!--- new value field--->                     

               <cfif NEWVALUE is not ''>        <!--- does not recognise the field NEWVALUE --->

                 <cfquery >        UPDATE table1 .. SET field1 = NEWVALUE </cfquery>     <!--- update value --->                       </cfquery>                                                                                                                          

              </cfif>                                                                    

          </tr>

       </cfloop>

     </table>        

</cfform>

</cfoutput>

Correct answer BKBK

With a query-generated table within a form, there is a lot going on all at once. So I don't quite understand what the code is doing. But I can see places where I can make a suggestion.

 

My suggestions assume that:

  • the form in the cfm page submits to the same page (CGI.SCRIPT_NAME). That is, it is its own action-page.
  • the variable newValue is a form field.
  • when you press the submit button, the form posts to the current page.
  • the update-query runs only when the form submits.
<cfoutput>

<cfform action="#CGI.SCRIPT_NAME#"  method="POST" name="FormX">    
	<table width="200" border="1" cellspacing="0" cellpadding="2">
	<cfloop query="table1">
		<tr>
			<td nowrap="nowrap">#field#</td> ....  <!--- display fields --->
			<td>New value</td><td><cfinput type="text" name="NEWVALUE" maxlength="20" size="20"></td>   <!--- new value field--->                     
			<cfif isDefined("FORM.NEWVALUE") and FORM.NEWVALUE is not ''>       
				 <cfquery >        
				 	UPDATE table1 
				 	SET field1 = '#FORM.NEWVALUE#' 
				 </cfquery>     <!--- update value --->                                                                                                                                                 
			 </cfif>                                                                    
		 </tr>
	 </cfloop>
	 </table>  
 
 <cfinput type="submit" name="submit" value="Submit">	             
</cfform>

</cfoutput>

 

1 reply

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
October 16, 2025

With a query-generated table within a form, there is a lot going on all at once. So I don't quite understand what the code is doing. But I can see places where I can make a suggestion.

 

My suggestions assume that:

  • the form in the cfm page submits to the same page (CGI.SCRIPT_NAME). That is, it is its own action-page.
  • the variable newValue is a form field.
  • when you press the submit button, the form posts to the current page.
  • the update-query runs only when the form submits.
<cfoutput>

<cfform action="#CGI.SCRIPT_NAME#"  method="POST" name="FormX">    
	<table width="200" border="1" cellspacing="0" cellpadding="2">
	<cfloop query="table1">
		<tr>
			<td nowrap="nowrap">#field#</td> ....  <!--- display fields --->
			<td>New value</td><td><cfinput type="text" name="NEWVALUE" maxlength="20" size="20"></td>   <!--- new value field--->                     
			<cfif isDefined("FORM.NEWVALUE") and FORM.NEWVALUE is not ''>       
				 <cfquery >        
				 	UPDATE table1 
				 	SET field1 = '#FORM.NEWVALUE#' 
				 </cfquery>     <!--- update value --->                                                                                                                                                 
			 </cfif>                                                                    
		 </tr>
	 </cfloop>
	 </table>  
 
 <cfinput type="submit" name="submit" value="Submit">	             
</cfform>

</cfoutput>

 

Inspiring
October 16, 2025

Thanks - now working ! 

BKBK
Community Expert
Community Expert
October 16, 2025

My pleasure!