javascript - 为什么我的代码显示错误的图像?

标签 javascript mysql coldfusion coldfusion-9 cfquery

我正在尝试自学冷聚变、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/

相关文章:

javascript - 推特 Bootstrap : Accordion plugin collides with Modal

php - Yii 范围 - 按相关模型过滤

c# - 用于导出到 Power BI 的 Azure Application Insights 分析查询

mysql - 了解在右侧元素上使用 AND 的左外连接,仍然会导致重复条目(使用 sql fiddle)

facebook - 调整 Facebook 个人资料图像的大小

xml - 无法支持 ColdFusion 生成的 XML 文件中的非 UTF-8 字符

javascript - AngularJS POST 失败 : Response for preflight has invalid HTTP status code 404

javascript - 在函数事件(onChange)中调用函数,来自在Leaflet和Vue.js中创建的Select

java - Railo Java 打印

javascript - XMLHttpRequest 服务器没有应答