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

时间:2014-04-07 19:01:25

标签: 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>

2 个答案:

答案 0 :(得分:2)

在你的popup()函数中,你正在引用images.image_ID。这将作为&#34; images.image_ID&#34;,而不是转换为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">

希望有所帮助。

答案 1 :(得分:0)

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

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