Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Does your debugging info show you the sql that caused this error?
Copy link to clipboard
Copied
Hi Dan,
Thanks for your help. No it doesn't show the sql that caused the error. It only goes on to say the resources that are available for example:
Resources:
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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#">