【问题标题】:How come my code displays the wrong image?为什么我的代码显示错误的图像?
【发布时间】:2014-04-07 19:01:25
【问题描述】:

我正在尝试自学冷融合、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>

【问题讨论】:

标签: javascript mysql coldfusion coldfusion-9 cfquery


【解决方案1】:

在您的 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">

希望对一些人有所帮助。

【讨论】:

  • 非常感谢您的帮助!!!在我对程序进行更改后,一切正常!!无论如何,你介意给我几个在线资源,我可以在哪里学习冷聚变吗? =)
【解决方案2】:

根据我评论的链接,您应该在cfoutput query属性中指定查询

<cfoutput query="myQuery3">
<img src="#image_fullpath#">
</cfoutput>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    相关资源
    最近更新 更多