我正在尝试自学冷聚变、javascript 和 mysql。我创建了一个页面,用户可以在其中单击图像,然后弹出窗口将显示完整图像。我的代码可以编译,但它总是显示我的图像数据库中的第一张图像。我在这上面花了很多时间,但无法弄清楚我错过了什么。有人可以提示我如何解决这个问题吗?预先感谢您的帮助!
文件 1:full_article_view.cfm
<!--- retrieve the full article as well as its images --->
<CFQUERY NAME="myQuery1" Datasource="mydb" >
SELECT articles.article_ID, articles.article_title, articles.article_author,
articles.article_date, articles.article_content, article_image_mapping.aim_articleID
FROM articles
INNER JOIN article_image_mapping ON articles.article_ID = article_image_mapping.aim_articleID
WHERE articles.article_ID = #URL.ID#
GROUP BY article_image_mapping.aim_articleID
</CFQUERY>
<CFQUERY NAME="myQuery2" Datasource="mydb" >
SELECT images.image_ID, images.image_thumbpath, images.image_fullpath, article_image_mapping.aim_articleID, article_image_mapping.aim_imageID
FROM images
INNER JOIN article_image_mapping ON images.image_ID = article_image_mapping.aim_imageID
WHERE article_image_mapping.aim_articleID = #URL.ID#
</CFQUERY>
<!DOCTYPE html>
<html>
<head>
<title>Learning</title>
<meta charset="UTF-8">
<script Language="JavaScript">
function popup()
{
settings="toolbar=yes,location=yes,directories=yes,"+
"status=no,menubar=no,scrollbars=yes,"+
"resizable=yes";
MyNewWindow = window.open("full_img.cfm?toshow=images.image_ID");
}
</script>
</head>
<body>
<!--- Page Title --->
<h3>Full Article View</h3>
<!--- Page Content --->
<div align="left">
<!--- Display article title, author, date, and full content --->
<cfoutput query="myQuery1">
<b>#ucase(myquery1.article_title)#</b>
<hr>
<p style="color:##848181; font-size:12px">#myquery1.article_author# :: #myquery1.article_date#</p>
#myquery1.article_content#<br/>
</cfoutput>
<br>
<!--- Display images associated with article--->
<cfoutput query= "myQuery2">
<a href="" onClick="popup();">
<img src="#myquery2.image_thumbpath#" alt="image thumbnail">
</a>
</cfoutput>
</div>
</body>
</html>
文件 2:full_img.cfm
<CFQUERY NAME="myQuery3" Datasource="mydb" >
SELECT image_ID, image_thumbpath, image_fullpath
FROM images
WHERE image_ID = #URL.toshow#
</CFQUERY>
<cfoutput>
<img src="#myquery3.image_fullpath#">
</cfoutput>
最佳答案
在您的 popup() 函数中,您引用了 images.image_ID。这将作为“images.image_ID”出现,而不是转换为 id。
您可以更改弹出窗口以传递您想要的 ID。
<script Language="JavaScript">
function popup(id)
{
settings="toolbar=yes,location=yes,directories=yes,"+
"status=no,menubar=no,scrollbars=yes,"+
"resizable=yes";
MyNewWindow = window.open("full_img.cfm?toshow=" + id);
}
</script>
然后在您的 HTML/CFML 代码中
<!--- Display images associated with article--->
<cfoutput query= "myQuery2">
<a href="" onClick="popup(#myQuery2.image_ID#);">
<img src="#myquery2.image_thumbpath#" alt="image thumbnail">
</a>
</cfoutput>
我知道您正在自学 ColdFusion。我建议您使用 cfqueryparam在查询中添加标记以防止 SQL 攻击。您需要在查询中围绕您的#URL.ID# 使用它。
所以这样:
...WHERE articles.article_ID = #URL.ID#
会是这样的:
...WHERE articles.article_ID = <cfqueryparam value = "#URL.ID#" cfsqltype = "cf_sql_integer">
希望对一些人有所帮助。
关于javascript - 为什么我的代码显示错误的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22920803/