Skip to main content
Participant
October 13, 2009
Question

Setting CFPARAM default value

  • October 13, 2009
  • 3 replies
  • 1742 views

Hi,

I am working on a shopping cart site that I did not design.

The cart currently has 1 page for each product category so there is about 15 pages.  I want to reduce it to one page and carry the cat_id through the URL.  I am doing this successfully, however, when it comes to 'Adding and Item to the Cart' it loses the value for the cat_id and returns an error.

The following is the code I am using:

<cfparam name="url.cat_id" default="" />

<cfquery name="catalogue" datasource="#rxxxxx#" username="#xxxxxx#" password="#xxxxxxx#">
SELECT *
FROM products

WHERE prod_cat_id = #url.cat_id#

</cfquery>

However, when I change the code to the following (the difference is bolded), the cart works as expected:

<cfparam name="url.cat_id" default="9" />

<cfquery name="catalogue" datasource="#rxxxxx#" username="#xxxxxx#" password="#xxxxxxx#">
SELECT *
FROM products

WHERE prod_cat_id = #url.cat_id#

</cfquery>

My question is, is there a way to set the cfparam default value with the URL.cat_id value?

Any suggestions?

    This topic has been closed for replies.

    3 replies

    Inspiring
    October 13, 2009

    Make sure that your "Add an Item to the Cart" link/form also passes the category ID via your URL cat_id variable (not via a form field).

    You may also want to consider using the <cfqueryparam> tag for added security in your query:

    WHERE prod_cat_id = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#url.cat_id#">

    Inspiring
    October 13, 2009

    What you are attempting is to set the value of the url variable to something you didn't receive.

    Before looking to set a default value, ask yourself some questions.  First, under what circumstances could the page be requested without the url variable.  Next, what do you want to happen when it does?

    October 13, 2009

    If the  prod_cat_id is an integer value, then the default value of cat_id can be set to zero. Or you can add a condition to execute the query only if the

    cat_id is not null. If you can specify the error thrown, it can give more idea about it.

    Participant
    October 14, 2009

    Hi,

    The error thrown is as follows:

    ERROR EXECUTING DATABASE QUERY

    Syntax error or access violation: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3

    Inspiring
    October 14, 2009

    Does your debugging info show you the sql that caused this error?