Skip to main content
Inspiring
November 17, 2022
Answered

Insert into when a page loads

  • November 17, 2022
  • 1 reply
  • 545 views

Hello So I have a page and when it loads I would like to have it insert into a table if a record is matched. In the body I have this:

 

<cfquery name="countlookup" Datasource="#application.datasource#" dbtype="ODBC">
SELECT id as idlx
FROM usercount
</cfquery>
<cfset idl = #countlookup.idlx#>
<cfif #idl# eq '449'>
<cfquery name="countlookup450" Datasource="#application.datasource#" dbtype="ODBC">
INSERT INTO usercount (id)
values (450)
</cfquery></cfif>

Issue I'm seeing is the page loads and doesn't insert anything in the usercount table. What am I messing up? I'm using Coldfusion 2021 woth SQL 2019.

 

Thanks

    This topic has been closed for replies.
    Correct answer EddieLotter

    What you're missing is a where clause in your select query.

    The query is very likely returning multiple rows and the first row does not match '449'.

    1 reply

    EddieLotter
    EddieLotterCorrect answer
    Inspiring
    November 17, 2022

    What you're missing is a where clause in your select query.

    The query is very likely returning multiple rows and the first row does not match '449'.

    taunntAuthor
    Inspiring
    November 18, 2022

    Yes you are correct. It's always the small things you miss. after adding where id = '449' it worked.

     

    Thanks

    EddieLotter
    Inspiring
    November 18, 2022

    You're welcome. Glad it was an easy fix.