Working with database

I tried like this but I am getting the same error. My DB has fields id, username, email. ID is set as primary key so I think you can omit it when you are inserting new values.

<cfset request.pageTitle = "Registration">

<cfif CGI.REQUEST_METHOD EQ "POST">
    <!--- Check if the form fields are defined --->
    <cfif IsDefined("form.username") and IsDefined("form.email")>
        <!--- Establish Connection to SQLite Database --->
        <cftry>
            <cfquery name="insertUser" datasource="mySQLiteDB">
                INSERT INTO users (username, email)
                VALUES (
                    <cfqueryparam value="#form.username#" cfsqltype="CF_SQL_VARCHAR">,
                    <cfqueryparam value="#form.email#" cfsqltype="CF_SQL_VARCHAR">
                )
            </cfquery>

            <!--- Check if the registration was successful --->
            <cfif insertUser.recordCount>
                <p class="style1">Registration successful!</p>
            <cfelse>
                <p class="style1">Registration failed. Please try again.</p>
            </cfif>
        <cfcatch type="database">
            <p class="style1">Database error occurred: #cfcatch.message#</p>
        </cfcatch>
        </cftry>
    <cfelse>
        <p class="style1">Form fields are not defined.</p>
    </cfif>
</cfif>

<p align="center" class="style2">REGISTRATION FORM</p>
<p class="style1">In Development process</p>

<div class="reg__container">
    <form class="form__container" method="post">
        <label class="form-label" for="username">Username:</label>
        <input class="form-input" type="text" id="username" name="username" placeholder="Enter your username" required>

        <label class="form-label" for="email">Email:</label>
        <input class="form-input" type="email" id="email" name="email" placeholder="Enter your email" required>

        <div class="form-checkbox-container">
            <input class="form-checkbox" type="checkbox" id="subscribe" name="subscribe">
            <label class="form-label" for="subscribe">Agree to our <a href="#">terms</a> of use.</label>
        </div>
        <button class="form-button" type="submit">Submit</button>
    </form>
</div>

yes, of course, because you’re making the same mistake: you can’t use recordcount on an insert query:

So what should I use then? I am not familiar with this.

If the insert doesn’t throw an error, it’s safe to assume it was successful.

There are errors mentioned above. But I don’t know how to resolve this

“yes, of course, because you’re making the same mistake: you can’t use recordcount on an insert query”

Ok, this is solved. I fixed it by removing that part of code which checks that thing. :laughing:

3 Likes