简体   繁体   English

ColdFusion和AJAX-更新数据库

[英]ColdFusion and AJAX - Updating Database

I'm wanting to have a system set up where users can go to a page where there's a list of items that they can rate (say, 1 through 5) from a drop-down. 我想建立一个系统,使用户可以转到一个页面,在该页面上可以从下拉菜单中评估(例如1到5)项目列表。 This list is going to get quite long thus it would be much more convenient if they could go through and rank each item without ever having to hit "Save." 该列表将变得很长,因此,如果他们无需单击“保存”就可以对每个项目进行排序并对其进行排名,将会更加方便。

I'm very much a novice when it comes to AJAX but I figure this can't be that difficult. 关于AJAX,我非常新手,但我认为这并不那么困难。 I found an answer in a different discussion that I think is quite relevant but it doesn't provide enough information for me to know exactly what to do with it. 在另一个讨论中找到了一个我认为非常相关的答案,但它没有提供足够的信息让我确切知道如何处理。

In short, how do I use AJAX, in congruence with jQuery and ColdFusion, to update the database without the need for a save/submit button? 简而言之,如何与jQuery和ColdFusion配合使用AJAX来更新数据库,而无需保存/提交按钮?

CLARIFICATION: I should have clarified that the user can rate the items - not rank. 澄清:我应该澄清的是,用户可以对项目进行评分-而不是排名。 Meaning that there's no #1, #2, #3, etc. Instead, each item can be rated on a scale from 1-5. 意味着没有#1,#2,#3等。相反,每个项目的评分范围为1-5。

Currently I'm basing everything off of ".change()" when the user makes a selection in the drop-down. 当前,当用户在下拉菜单中进行选择时,我将所有内容基于“ .change()”。 At which point I have two jQuery variables that are set to the "ID" of the item changed as well as its new "rating." 在这一点上,我有两个jQuery变量,它们分别设置为更改的项目的“ ID”及其新的“评级”。 I just need to find a way to use these two variables to update the table in the database. 我只需要找到一种使用这两个变量来更新数据库中表的方法。

Update 更新

On the main page, I now have a simple jQuery AJAX post call: $.post('update.cfc', {ID: inputID, rating: selRat}) that is triggered .onChange() . 在主页上,我现在有一个简单的jQuery AJAX帖子调用: $.post('update.cfc', {ID: inputID, rating: selRat}) ,它由.onChange()触发。 "inputID" is the ID of the entry that I want to update in the database and "selRat" is the selected rating in the drop-down. “ inputID”是我要在数据库中更新的条目的ID,而“ selRat”是下拉菜单中的选定等级。

 <cfcomponent output="false"> <cffunction name="updateRating" access="remote" output="false"> <cfupdate datasource="#session.db#" name="update"> UPDATE ajaxTest SET rating = #FORM.rating# WHERE ID = #FORM.ID# </cfupdate> </cffunction> </cfcomponent> 

I'm currently getting a 500 (Internal Server Error) . 我目前收到500 (Internal Server Error)

Thoughts/suggestions? 思考/建议吗?

Have you tried submitting the traditional way to this form to see what happens? 您是否尝试过将传统方式提交此表单以查看会发生什么情况? You can also add some simple debugging like writing to a file to log the executed attempt? 您还可以添加一些简单的调试功能,例如写入文件以记录执行的尝试?

One thing that stands out to me: Should the <cfupdate opening and closing tag be <cfquery I don't use cfupdate or cfinsert, but I don't think any cf flavor supports the syntax you're trying to use, which leads me to believe it's an easy mistake. 对我来说很重要的一件事:如果<cfupdate开始和结束标记是<cfquery我不使用cfupdate或cfinsert,但是我不认为任何cf风格都支持您尝试使用的语法,这导致我相信这是一个简单的错误。

On another note, that query could be especially dangerous, and sorry to go off on a tangent.. 另外,该查询可能特别危险,很遗憾切线。

If I submitted "0; DROP TABLE Users" for the value of ID, your Users table disappears (if you have a users table). 如果我为ID的值提交了“ 0; DROP TABLE Users”,则您的Users表消失(如果您有一个users表)。 I could also pass other common table names like Members, News, Pages, Content, CMS, Transactions. 我还可以传递其他常用表名称,例如“成员”,“新闻”,“页面”,“内容”,“ CMS”,“交易”。 (I wouldn't do this, but you never know when another user might.) (我不会这样做,但是您永远不知道什么时候其他用户可以。)

There's a really easy solution, <cfqueryparam> . 有一个非常简单的解决方案, <cfqueryparam> I'll let you research the tag, but I'll show you how to alter that particular query. 我将让您研究标记,但是我将向您展示如何更改该特定查询。

<cfquery datasource="#session.db#" name="update">
    UPDATE ajaxTest
    SET rating = <cfqueryparam cfsqltype="cf_sql_integer" value="#form.rating#">
    WHERE ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#form.id#">
</cfquery>

CFQUERYPARAMing your variables stops the risk of this and passes the variable values as text rather than sql. CFQUERYPARAMing变量可以避免这种风险,并将变量值作为文本而不是sql传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM