2013-09-16 27 views
1

我有一個表格內填充了數據庫中的文件名列表。表中的每一行都有一個複選框。我希望能夠檢查表中的多行,然後提交表單。提交後,每個數據庫行將根據檢查的文件名被刪除。我也需要能夠從服務器上刪除這個文件。我正在使用ColdFusion,並且我知道這將需要某種類型的cfloop,但我不確定實現它的最佳方式。使用Coldfusion通過文件名刪除多行

這裏是表單的代碼。

<cfform name="mainform" 
    action="deletingFiles.cfm?action=deleteDoc&teacherid=#url.teacherid#" method="post"> 

    <table border="0" width="50%" id="product-table"> 
    <tr> 
     <th>Check</th> 
     <th> Description </th> 
     <th>Date</th> 
    </tr> 
    <cfoutput query="delete"> 
    <tr> 
     <td><input type="checkbox" name="DeleteID" value="#file_name#"></td> 
     <td><a href= "deletingFiles.cfm?action=editDoc">#description#</a></td> 
     <td>#DateFormat(date, "mmm-dd-yyyy")#</td> 
    </tr> 
    </cfoutput> 
    </table> 

    <center> 
     <button name="passchk" class="button blue" type="submit" > 
      <p>&nbsp;&nbsp;Delete Checked Items&nbsp;&nbsp;</p> 
     </button> 
    </center> 
</cfform> 

這是我從數據庫和服務器中刪除文件的代碼。現在它只會刪除一個文件。

<cfif URL.action is "deleteDoc"> 
    <cfquery name="deleteDoc" datasource="test" username="" password=""> 
     delete from testdoc where file_name = '#form.DeleteID#' 
    </cfquery> 

    <cffile action = "delete" 
     file = "f:\inetpub\wwwroot\test\#form.DeleteID#"> 
</cfif> 

預先感謝您的幫助,您可以提供任何幫助!

回答

1

最基本的答案是這樣的:

<!--- This performs SQL delete even if there is no corresponding file ---> 
<cfquery name="deleteDoc" datasource="test" username="" password=""> 
    delete 
    from testdoc 
    where file_name IN (<cfqueryparam cf_sql_type="varchar" 
     value="#form.DeleteID#" list="yes">) 
</cfquery> 


<cfloop index="i" list="#form.DeleteID#"> 

    <!--- This code is vulnerable to attacks consider filtering out input ---> 
    <cffile action = "delete" 
     file = "f:\inetpub\wwwroot\test\#i#"> 

</cfloop> 
+0

我想你的意思是使用'IN(...)'不'=' – Leigh

+0

這個工作太棒了!非常感謝你的幫助! – stat8