Question : In a database, using a ColdFusion application, I need to assign documents to document topics.

Database: MS SQL Server 2005
ColdFusion version 8

Hello. I've been working on this task for a while and have reached a point at which I need to ask for help.

Task in brief: In a database, I need to assign documents to document topics.

History:
Last month, gdemaria helped me -- very patiently -- to construct an application that allows a user to upload and manage documents in a database. The application works perfectly and I learned a ton while gdemaria guided me in building it. The application allows a user to create a document; upload files (DOC, PDF, images) that associate with that document, and manage metadata that pertain to the document.

Now, I need to assign uploaded documents to document topics.

Task in detail:

Basically I need to add to my application the ability to update a table: tbl_Document_Has_Topic.

My three working tables are:

tbl_CEP_Documents (this table holds metadata about CEP Documents) PK: DocumentID
tbl_CEP_Document_Topic (this table holds a list of all CEP Document Topics) PK: DocumentTopicID
tbl_Document_Has_Topic (this table relates tbl_CEP_Documents to tbl_CEP_Document_Topic)

tbl_Document_Has_Topic needs only two columns:
DocumentID
DocumentTopicID

* DocumentID is the PK in tbl_CEP_Documents.
* DocumentTopicID is the PK in tbl_CEP_Document_Topic.

..... If I assign a DocumentTopicID to a DocumentID, then that Document will be assigned to the correct Document Topic(s).

That is the function of tbl_Document_Has_Topic: to assign DocumentTopicID(s) to a DocumentID.

My task is to include, in my document edit interface, a select menu that allows a user to choose Document Topics to assign to a document.

I added the select menu. Please see an example here:

http://ebwebwork.com/cep/admin/insert_update.cfm?DocumentID=27

....Notice the select menu "Assign this Document to CEP Topics". The select menu simply loops through the 24 possible CEP document topics. Thus:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
<!--- this query requests values from columns DocumentTopicID and DocumentTopic in tbl_CEP_Document_Topics --->
 <cfquery datasource="#ds#" name="GetDocumentTopics">
SELECT DocumentTopicID, DocumentTopic
FROM tbl_CEP_Document_Topic
ORDER BY DocumentTopic
  </cfquery>
 
    <p><strong>Assign this Document to CEP Topics</strong><br />
      <span class="small">Choose at least one topic. To select more than one topic, hold the Ctrl key and select topics with the mouse cursor.</span></p>
  
   <select name="SelectDocumentTopics" value="#form.SelectDocumentTopics#" message="Please choose at least one Document Topic from the Document Topics Select Menu" required="Yes" validateAt="onSubmit,onServer" size="5">

    <!--- list DocumentTopicID and DocumentTopic in Select List of Document Topics --->
    <option value="" selected="selected">Choose topic:</option>
<cfoutput query="GetDocumentTopics">
<option value="#GetDocumentTopics.DocumentTopicID#">#GetDocumentTopics.DocumentTopic# (DocumentTopicID: #GetDocumentTopics.DocumentTopicID#)</option>
</cfoutput>
 </select>


That Select menu works well. Notice that in the select menu each DocumentTopic is paired with its correct DocumentTopicID.

So, I need to build a new query that updates table tbl_Document_Has_Topic, using the selections from the Select menu.

Where to place this query, and what form shall the query take?

A new document will need to be assigned to document topics; and, existing documents will need to change topics now and then.

In the application I already have queries that insert a new document, and update an existing document. Do I need to modify those existing queries, so that the queries will update tbl_Document_Has_Topic with appropriate DocumentID and DocumentTopicID values? That would seem like the way to do it. And, I would need to create an inner join (I think) to update the table, tbl_Document_Has_Topic.

Am I thinking about this correctly?

I am very grateful for any advice.

I append my working code.

Thank you as always.

Eric B
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
<!--- Page header --->
<cfinclude template="cep_header.cfm" />

 <!--- Set default value for FileID in scope FORM --->
<cfparam name="form.FileID" default="">




 <!--- Set default value for DocumentID in scope URL --->
<cfparam name="URL.DocumentID" default="">

 <!--- Define DocumentID in scope FORM, then set form.DocumentID equal to the DocumentID passed in the URL: for use later in the application --->
<cfparam name="form.DocumentID" default="#URL.DocumentID#">


 <!--- Set datasource --->
 <cfset ds="ebwebwork">
  
 <!---- begin CFTRY; catch any errors, whether you throw them or the database does; and to test that file were uploaded successfully, or not  ---->
 <cftry>  
 

<!---- populate this with an error message ---->
<cfset variables.error = ""> 


        <!--- BEGIN: Save action --->


<!--- begin form.doSave --->

<!--- when the user clicks the Save Button, these events can occur: Add a document; Update a document; Delete a Document; Delete a file that is associated with a document --->

<cfif IsDefined("FORM.doSave")>


<!--- make sure that documentTitle and documentType are entered --->  
    
	<cfif len(form.DocumentTitle) eq 0>
			  <cfthrow message="Document Title is required">
	</cfif>
    
    <cfif len(form.DocumentType) eq 0>
			  <cfthrow message="Document Type is required">
	</cfif>


 <!--- in this query select NOTHING from table tbl_CEP_documents, and simply check if DocumentTitle exists --->
 
 <cfquery datasource="#ds#" name="CheckDocumentTitle">
  SELECT 'Nothing' FROM tbl_CEP_Documents
  WHERE DocumentTitle = <CFQUERYPARAM CFSQLTYPE="cf_sql_varchar" VALUE="#Form.DocumentTitle#">
  AND DocumentID <> <CFQUERYPARAM CFSQLTYPE="cf_sql_integer" VALUE="#val(Form.DocumentID)#">
  </cfquery>
   
 
  
   <!--- if DocumentTitle exists, display error; refuse record insert --->
   
	<cfif CheckDocumentTitle.recordcount GT 0>
		   <cfthrow message="The Document Title is already taken; please enter another title">
	</cfif>
    
    <!--- begin statements to update, insert database records --->  
   
    <!--- begin CFIF val(form.DocumentID) --->
    
    <!--- the action is UPDATE; a DocumentID Exists --->
    
				<cfif val(form.DocumentID)>
    
  			  <cfquery name="UpdateDocument" datasource="#ds#">
				  UPDATE tbl_CEP_Documents
				  SET   DocumentTitle = <cfqueryparam cfsqltype="cf_sql_varchar"  value="#Trim(form.DocumentTitle)#">,
					DocumentType = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentType)#">,
					DocumentAuthor = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAuthor)#">,
				    DocumentAbstract = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAbstract)#">,
				    DocumentPublicationDate = <cfqueryparam cfsqltype="cf_sql_date" value="#Trim(form.DocumentPublicationDate)#">,
				    DateRecordModified = <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">
				  WHERE DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">
			</cfquery>



    <!--- CFELSE: DocumentID does not exist, then insert new document --->
				<cfelse> 
    
    
	
				<!---- query to insert new document into tbl_CEP_Documents ---->
					<cfquery name="InsertDocument" datasource="#ds#">
				 INSERT INTO tbl_CEP_Documents
     					(
			            DocumentTitle,
            		    DocumentType,
                		DocumentAuthor,
		                DocumentAbstract,
        		        DocumentPublicationDate,
                		SSMA_TimeStamp
		                )
			     VALUES(
						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentTitle)#">,
						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentType)#">,
						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAuthor)#">,
						  <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(form.DocumentAbstract)#">,
						  <cfqueryparam cfsqltype="cf_sql_date" value="#Trim(form.DocumentPublicationDate)#">,
		     			  <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">
				         )         
					</cfquery>

		<!--- end queries to update or insert database records ---> 




<!--- get the primary key from table tbl_CEP_Documents, for use later --->
    <cfquery name="GetPrimaryKey" datasource="#ds#">
          select MAX(DocumentID) as ID
           FROM tbl_CEP_Documents
      </cfquery>

<!--- set value of form.DocumentID equal to GetPrimaryKey.ID --->
        <cfset form.DocumentID = GetPrimaryKey.ID>






           <!--- end CFIF val(form.DocumentID) -- if a document needed to be updated, or added, then it was done --->
					    </cfif>  
    
    
    
<!--- remember CFIF IsDefined("FORM.doSave") is still defined -- remember to close the CFIF later --->
    

    
          
				<!--- test to see whether or not a file is being uploaded -- does form field FileName contain a file? --->         
  
		    	<!--- BEGIN: if form field FileName contains a file, then upload the file and the file's metadata --- this function updates the tbl_CEP_Files--->
      			<CFIF form.fileName is not "">
			    <!--- BEGIN: upload a file using CFFILE --->       
     			<!--- cffile upload --->       
   			      <cffile action="upload" filefield="FileName" destination="c:\upload\cep-dc.org" nameconflict="overwrite">

			<cfif listFindNoCase("doc,docx,jpg,jpeg,png,gif,pdf,ppt,xls,xlsx,txt", cffile.serverFileExt) eq 0>
				    <cfthrow message="Your file did not upload. You may upload only permitted file types.">
			</cfif>
            

			<!---- query to insert new record into tbl_CEP_Files, with a file  (DOC, JPEG, PDF, etc.) to upload ---->
			<cfquery name="InsertFile" datasource="#ds#">
			 INSERT INTO tbl_CEP_Files
     			(
                DocumentID,
                FileName,
				FileExtension,
				FileType,
				FileSize,
                isDeleted
                )
		     VALUES(
        		  <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">
				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.clientFile#">
				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.clientFileExt#">
				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.contentType#">
				  ,<cfqueryparam cfsqltype="cf_sql_varchar" value="#cffile.fileSize#">
                  ,0
		           )         
				</cfquery>  
  
		   <!--- end query to insert new record into tbl_CEP_Files --->  
  
           
              <!--- delete a file --->
               <!--- this section must be placed within the Save Action --->
           
         
					   <!--- begin CFELSEIF isdefined(form.deleteFileID) --->
		 <cfelseif isDefined("form.deleteFileID")>        
    	   <cfquery name="DeleteFile" datasource="#ds#">
       			UPDATE tbl_CEP_Files
           		SET IsDeleted = 1
        		WHERE FileID in (<cfqueryparam cfsqltype="cf_sql_integer" value="#form.deleteFileID#" list="Yes">)
      		</cfquery>




		    <!--- END: CFIF form.fileName is not "" --->
					</cfif>
                    
  
		<!--- if insert record succeeded, and / or file upload succeeded, then go to next page --->
				     <cflocation url="index.cfm" addtoken="no">
                         
                    
    <!--- END: Save action --->
    


    
    
        <!--- BEGIN: Delete action --->


			   <!--- delete a database record --->
					   <!--- begin CFELSEIF isdefined(form.doDelete) --->
					        <cfelseif isDefined("form.doDelete")>        
					        <!---- query to delete record ---->
					       	<cfquery name="DeleteDocument" datasource="#ds#">
					         DELETE FROM tbl_CEP_Documents
					         WHERE DocumentID = <cfqueryparam cfsqltype="cf_sql_integer" value="#val(form.DocumentID)#">
					        </cfquery>
              
 					        <!---- after delete, go to index page ---->
						 <cflocation url="index.cfm" addtoken="no">

	    <!--- END: Delete action --->
        
        
            
    		

  <!--- end form.doSave --->
</cfif>




       <!--- this CFCATCH will trap errors -- the ones you threw or just regular database issues --->
<cfcatch type="Any">
     <cfset variables.error = cfcatch.message>
     <cfrethrow>
  </cfcatch>

  
  
  
		  <!--- end CFTRY --->  
			</cftry>



<!--- fetch the data from the database only when there are no errors.
      if an error exists, then let the form variables pass back into the form to display ---->
 
  <cfif len(variables.error) eq 0>
    
			  <!--- get data from table tbl_CEP_Documents and convert the data into form variables --->
			  <cfquery name="getDocumentDetails" datasource="#ds#">
				    select * from tbl_CEP_Documents where DocumentID = #val(form.DocumentID)#
			  </cfquery>

  			<cfloop index="aCol" list="#getDocumentDetails.columnList#">
			       <cfset "form.#aCol#" = getDocumentDetails[aCol][getDocumentDetails.currentRow]>
			  </cfloop>
    
	</cfif>


<!--- if there an error, display error in human readable form --->

<cfif len(variables.error)> 
			 <cfoutput>
			 <div style="border: 1px solid red; padding: 10px; margin:20px; width:400px;">#variables.error#</div>
			 </cfoutput>
</cfif>


				<!----- if record already exists (it will have an ID) then update it; otherwise, add new record... ----->
				<cfif val(form.documentID)>
					  <cfset FormTitle="Update a Document">
					  <cfset ButtonText="Update">
  
				<cfelse>
						<cfset FormTitle="Add a Document">
						<cfset ButtonText="Add Document">

				</cfif>

  
				<!--- Add or Update Document Form begins here --->
				<cfform method="post" enctype="multipart/form-data">


 <!--- Embed documentID (PK) and fileID as hidden fields --->
 <cfoutput>
<input type="hidden" name="documentID" value="#form.documentID#" />
<input type="hidden" name="fileID" value="#form.fileID#" />
   </cfoutput>

   <cfoutput>
<h2>#FormTitle#</h2>
   </cfoutput>


<table class="table_admin">

 <tr>
  <td>
<p><strong>Document ID: <cfoutput>#URL.DocumentID#</cfoutput></strong></p>
  </td>
 </tr>

 <tr>
  <td>
   <p><strong>Document Title</strong></p>
   <cfinput type="Text"
            name="DocumentTitle"
            value="#form.DocumentTitle#"
            message="Document title is required! Please restrict the Document title to 100 characters or fewer."
            required="Yes"
            validateAt="onSubmit,onServer"
            size="50"
            maxlength="100" />
  </td>
 </tr>
 <tr>
  <td>
   <p><strong>Document Type</strong></p>
   <select name="DocumentType" value="#form.DocumentType#" message="Please choose Document Type from the Document Type Select Menu" required="Yes" validateAt="onSubmit,onServer">            <option value="Report">Report</option>
            <option value="Press Release">Press Release</option>
            <option value="Article">Article</option>
            <option value="Summary">Summary</option>
            <option value="Letter">Letter</option>
            <option value="Audio Transcript">Audio Transcript</option>
            <option value="Text Transcript">Text Transcript</option>
   </select>
  </td>
 </tr>


 <tr>
  <td>
   <p><strong>Document Author</strong></p>
   <cfinput type="Text"
            name="DocumentAuthor"
            value="#form.DocumentAuthor#"
            message="Enter Document Author Name"
            required="yes"
            validateAt="onSubmit,onServer"
            size="10"
            maxlength="100" />
  </td>
 </tr>
 
 
<!--- this query requests values from columns DocumentTopicID and DocumentTopic in tbl_CEP_Document_Topics --->
 <cfquery datasource="#ds#" name="GetDocumentTopics">
SELECT DocumentTopicID, DocumentTopic
FROM tbl_CEP_Document_Topic
ORDER BY DocumentTopic
  </cfquery>
 
  <tr>
  <td>
    <p><strong>Assign this Document to CEP Topics</strong><br />
      <span class="small">Choose at least one topic. To select more than one topic, hold the Ctrl key and select topics with the mouse cursor.</span>
    </p>
  
   <select name="SelectDocumentTopics" value="#form.SelectDocumentTopics#" message="Please choose at least one Document Topic from the Document Topics Select Menu" required="Yes" validateAt="onSubmit,onServer" size="5">

    <!--- list DocumentTopicID and DocumentTopic in Select List of Document Topics --->
    <option value="" selected="selected">Choose topic:</option>
<cfoutput query="GetDocumentTopics">
<option value="#GetDocumentTopics.DocumentTopicID#">#GetDocumentTopics.DocumentTopic# (DocumentTopicID: #GetDocumentTopics.DocumentTopicID#)</option>
</cfoutput>
 </select>
  </td>
 </tr>
 

 <tr>
  <td>
   <p><strong>Document Abstract</strong></p>
   <cfoutput>
   <textarea name="DocumentAbstract" cols="40" rows="5" wrap="virtual">#form.DocumentAbstract#</textarea>
   </cfoutput>
  </td>
 </tr>
 
 
 <tr>
  <td>
   <p><strong>Publication Date (use form MM/DD/YYYY)</strong></p>
   <cfinput type="Text"
            name="DocumentPublicationDate"
            value="#form.DocumentPublicationDate#"
            message="Publication Date must be a valid date!"
            required="no"
            validate="date"
            validateAt="onSubmit,onServer"
            size="10"
            maxlength="10" />
  </td>
 </tr>


<tr>
  <td>
  

   <!--- display the files currently associated with the document --->
<p>Below, please see the files currently associated with Document ID <strong><cfoutput>#URL.DocumentID#</cfoutput></strong>. If there are no files associated with the document, then no files will appear below. You have the option to upload a file (PDF, DOC, image file), which will associate the file with Document ID <strong><cfoutput>#URL.DocumentID#</cfoutput></strong>.</p>

<cfquery name="getFiles" datasource="#ds#">
    select * from tbl_CEP_files where DocumentID = #val(form.DocumentID)# and isDeleted = 0
  </cfquery>
  <cfif getFiles.recordCount>
   <table class="table_admin">
     <tr>
      <th>File Name</th>
      <th>Delete</th>
     </tr>
     <cfloop query="getFiles"><cfoutput>
     <tr>
       <td>#getFiles.FileName#</td>
       <td><input type="Checkbox" name="deleteFileID" value="#getFiles.fileID#"></td>
     </tr></cfoutput>
     </cfloop>
   </table>
  <cfelse>
   <p><strong>No Files are Attached to this Document.</strong></p>
  </cfif>



  <!--- input field for file upload --->

<cfinput type="file" size="25" accept="application/msexcel,application/msword,application/pdf,image/gif,image/jpeg,image/x-png" name="FileName" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" maxlength="255" />




  <!--- submit form to ColdFusion for processing; this is the DoSave function, which will add or edit a Document record --->
<cfoutput>
  <input name="doSave" type="submit" value="#ButtonText#" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'" />
</cfoutput>


<cfif val(form.documentID)>

<hr />

<div class="graybox align-center">
  <p>The button, below, deletes this document, its database record, and all associated files. Use carefully.</p>
</div>


<!--- submit form to ColdFusion for processing; this is the DoDelete function, which deletes a Document record --->
<cfoutput>
  <div class="align-center">
  <input name="doDelete" type="submit" value="Delete This Document" class="btn" onmouseover="this.className='btn btnhov'" onmouseout="this.className='btn'"  onclick="return confirm('Are you sure you wish to delete this document? After you delete it it is gone forever.')" />
  </div>
</cfoutput>

<cfelse>

</cfif>

</cfform>


<!--- Page footer --->
<cfinclude template="cep_footer.cfm" />

Answer : In a database, using a ColdFusion application, I need to assign documents to document topics.

> <cfquery name="GetPrimaryKey" datasource="#ds#">
> select MAX(DocumentID) as ID
>           FROM tbl_CEP_Documents
> </cfquery>

If you're using that query to retrieve the ID of a newly INSERTed document ... don't ;-) In an environment with a lot of concurrent users it's likely to return the wrong ID.  

The best way to get the value of an auto incrementing column is to use CF8's cfquery "result" attribute.  After an INSERT, CF populates the result structure with the new record ID. The key name varies depending on your db type

Example: For an MS SQL database the key name is IDENTITYCOL

<cfquery name="InsertDocument" datasource="#ds#" result="newDocument">
   INSERT INTO tbl_CEP_Documents (...columns....). VALUES (......)
</cfquery>

<cfoutput>
     The new documentID is #newDocument.IDENTITYCOL#
</cfoutput>


> I already have queries that  insert a new document, and update an existing document...
> Do I  need to modify those existing queries

Only the INSERT  query, so you can retrieve the newly inserted DocumentID.  Just use the "result" attribute as described above.  With an UPDATE, you already have the ID (ie #form.DocumentID#)

Since you're modifying a separate table, you'd use a separate cfquery.  For new documents, first do the INSERT into  tbl_CEP_Documents.  Once you have the new documentID, you can use it to insert the selected topics into your second table.

Since the topic ID's come from your tbl_CEP_Document_Topic, you can use a SELECT to grab add all of the selected topics from that table, and insert the values into tbl_Document_Has_Topic in one fell swoop.  

For both the inserts and updates, be sure to wrap the two queries in a transaction.  So either both queries succeed or both fail ... together.

(Again, I'm assuming MS SQL is the database ..)

       <!--- use a transaction so both queries are processed together  --->
        <cftransaction>
<cfquery name="InsertDocument" datasource="#ds#" result="newDocument">
    INSERT INTO tbl_CEP_Documents (...columns....). VALUES (......)
</cfquery>
<!--- get the selected topics, and insert them into the 2nd table along with the document ID --->
            <cfquery name="InsertDocumentTopics" datasource="#ds#">
                   INSERT INTO
tbl_Document_Has_Topic  ( DocumentID,  DocumentTopicID )
                  SELECT
                   <cfqueryparam value="#newDocument.newDocument.IDENTITYCOL#" cfsqltype="cf_sql_integer">
                  , DocumentTopicID
                  FROM  tbl_CEP_Document_Topic
                 WHERE   DocumentTopicID  IN (
                     <cfqueryparam value="#form.SelectDocumentTopics#"  cfsqltype="cf_sql_integer" list="true">
                  )
               </cfquery>
        </cftransaction>

Updates are a little different. More about that in a minute.
 

Random Solutions  
 
programming4us programming4us