Tuesday, July 14, 2026

.::: Sample Access Database MariaDB via Frontend & Backend using Lucee with access login Application.cfc, users save in table users, insert, update, delete with confirm :::.

 
 
hanya berubah pembelian_all.cfm n lanjutan dari https://teguhth.blogspot.com/2026/07/sample-access-database-mariadb-via_01669209479.html

1. create user for login

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(100) NOT NULL,
    nama VARCHAR(100),
    aktif CHAR(1) DEFAULT 'Y'
);

INSERT INTO users(username,password,nama,aktif)
VALUES
('admin','123456','Administrator','Y');

INSERT INTO users(username,password,nama,aktif)
VALUES
('aisyah','123456','Administrator','Y');

INSERT INTO users(username,password,nama,aktif)
VALUES
('teguh','123456','readonly','Y');
 
2. Struktur file

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# ls -lh Application.cfc login.cfm logout.cfm pembelian_all.cfm
-rw-r--r-- 1 root root  730 Jul 13 16:32 Application.cfc
-rw-r--r-- 1 root root 1.6K Jul 13 14:57 login.cfm
-rw-r--r-- 1 root root   76 Jul 13 14:58 logout.cfm
-rw-r--r-- 1 root root 4.1K Jul 13 16:55 pembelian_all.cfm
[root@teguhth-all ROOT]#


3. Buat Application.cfc

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat Application.cfc
<cfcomponent output="false">

    <cfset this.name = "PembelianApp">
    <cfset this.sessionManagement = true>
    <cfset this.sessionTimeout = CreateTimeSpan(0,8,0,0)>

    <cffunction name="onRequestStart"
                output="false"
                returntype="boolean">

        <cfargument name="targetPage">

        <cfset page = ListLast(arguments.targetPage,"/")>

        <!-- halaman tanpa login -->
        <cfif ListFindNoCase("login.cfm",page)>
            <cfreturn true>
        </cfif>

        <cfif NOT StructKeyExists(session,"login")>

            <cflocation
                url="login.cfm"
                addtoken="false">

        </cfif>

        <cfreturn true>

    </cffunction>

</cfcomponent>
[root@teguhth-all ROOT]#

4. Buat login.cfm

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat login.cfm
<cfsetting showdebugoutput="false">

<cfif StructKeyExists(form,"username")>

    <cfquery
        datasource="dbteguhth"
        name="qUser">

        SELECT
            username,
            password,
            nama
        FROM users
        WHERE username=
        <cfqueryparam
            value="#form.username#"
            cfsqltype="cf_sql_varchar">

        AND aktif='Y'

    </cfquery>

    <cfif qUser.recordcount EQ 1>

        <cfif qUser.password EQ form.password>

            <cfset session.login=true>
            <cfset session.username=qUser.username>
            <cfset session.nama=qUser.nama>

            <cflocation
                url="pembelian_all.cfm"
                addtoken="false">

        <cfelse>

            <cfset pesan="Password Salah">

        </cfif>

    <cfelse>

        <cfset pesan="Username Tidak Ditemukan">

    </cfif>

</cfif>

<!DOCTYPE html>

<html>

<head>

<title>Login</title>

<style>

body{
font-family:Arial;
background:#f5f5f5;
}

.login{
width:350px;
margin:100px auto;
padding:20px;
background:white;
border:1px solid #ddd;
}

input{
width:100%;
padding:8px;
margin-bottom:10px;
}

button{
width:100%;
padding:10px;
}

</style>

</head>

<body>

<div class="login">

<h2>Login</h2>

<cfif IsDefined("pesan")>

<font color="red">

<cfoutput>#pesan#</cfoutput>

</font>

<br><br>

</cfif>

<form method="post">

Username

<input
type="text"
name="username"
required>

Password

<input
type="password"
name="password"
required>

<button
type="submit">

Login

</button>

</form>

</div>

</body>

</html>
[root@teguhth-all ROOT]#

 


5. Buat logout.cfm

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat logout.cfm
<cfset StructClear(session)>

<cflocation
url="login.cfm"
addtoken="false">
[root@teguhth-all ROOT]#


6. Ubah pembelian_all.cfm

[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat pembelian_all.cfm
<cfsetting showdebugoutput="false">

<!--- Action --->
<cfparam name="url.action" default="">
<cfparam name="url.kode" default="">

<!--- INSERT / UPDATE --->
<cfif StructKeyExists(form,"btnSimpan")>

    <cfif form.mode EQ "insert">

        <cfquery datasource="dbteguhth">
        INSERT INTO pembelian
        (
            KODE_PEMBELIAN,
            KODE_BARANG,
            KODE_CUSTOMER,
            TANGGAL_PEMBELIAN,
            JUMLAH_PEMBELIAN
        )
        VALUES
        (
            <cfqueryparam value="#form.KODE_PEMBELIAN#" cfsqltype="cf_sql_varchar">,
            <cfqueryparam value="#form.KODE_BARANG#" cfsqltype="cf_sql_varchar">,
            <cfqueryparam value="#form.KODE_CUSTOMER#" cfsqltype="cf_sql_varchar">,
            <cfqueryparam value="#form.TANGGAL_PEMBELIAN#" cfsqltype="cf_sql_date">,
            <cfqueryparam value="#form.JUMLAH_PEMBELIAN#" cfsqltype="cf_sql_integer">
        )
        </cfquery>

    <cfelse>

        <cfquery datasource="dbteguhth">
        UPDATE pembelian
        SET
            KODE_BARANG=<cfqueryparam value="#form.KODE_BARANG#" cfsqltype="cf_sql_varchar">,
            KODE_CUSTOMER=<cfqueryparam value="#form.KODE_CUSTOMER#" cfsqltype="cf_sql_varchar">,
            TANGGAL_PEMBELIAN=<cfqueryparam value="#form.TANGGAL_PEMBELIAN#" cfsqltype="cf_sql_date">,
            JUMLAH_PEMBELIAN=<cfqueryparam value="#form.JUMLAH_PEMBELIAN#" cfsqltype="cf_sql_integer">
        WHERE
            KODE_PEMBELIAN=<cfqueryparam value="#form.KODE_PEMBELIAN#" cfsqltype="cf_sql_varchar">
        </cfquery>

    </cfif>

    <cflocation url="pembelian_all.cfm" addtoken="false">

</cfif>

<!--- DELETE --->
<cfif url.action EQ "delete">

    <cfquery datasource="dbteguhth">
    DELETE FROM pembelian
    WHERE KODE_PEMBELIAN=
    <cfqueryparam value="#url.kode#" cfsqltype="cf_sql_varchar">
    </cfquery>

    <cflocation url="pembelian_all.cfm" addtoken="false">

</cfif>

<!--- EDIT --->
<cfif url.action EQ "edit">

    <cfquery name="qEdit" datasource="dbteguhth">
    SELECT *
    FROM pembelian
    WHERE KODE_PEMBELIAN=
    <cfqueryparam value="#url.kode#" cfsqltype="cf_sql_varchar">
    </cfquery>

    <cfset mode="update">

<cfelse>

    <cfset mode="insert">

</cfif>

<!--- LIST DATA --->
<cfquery name="qPembelian" datasource="dbteguhth">
SELECT *
FROM pembelian
ORDER BY KODE_PEMBELIAN
</cfquery>

<html>

<head>

<title>CRUD Pembelian</title>

<script>

function konfirmasiSimpan() {

    var mode = document.getElementById("mode").value;

    if (mode == "insert") {
        return confirm("Yakin ingin menambah data?");
    } else {
        return confirm("Yakin ingin mengubah data?");
    }

}

</script>

</head>

<body>

Selamat Datang
<b><cfoutput>#session.username# (#session.nama#)</cfoutput></b>
|
<a href="logout.cfm">Logout</a>

<hr>

<h3>Form Pembelian</h3>

<form method="post" onsubmit="return konfirmasiSimpan();">

<input
type="hidden"
id="mode"
name="mode"
value="<cfoutput>#mode#</cfoutput>">

Kode Pembelian<br>

<input
name="KODE_PEMBELIAN"
value="<cfoutput><cfif mode EQ 'update'>#qEdit.KODE_PEMBELIAN#</cfif></cfoutput>"
<cfif mode EQ "update">readonly</cfif>>

<br><br>

Kode Barang<br>

<input
name="KODE_BARANG"
value="<cfoutput><cfif mode EQ 'update'>#qEdit.KODE_BARANG#</cfif></cfoutput>">

<br><br>

Kode Customer<br>

<input
name="KODE_CUSTOMER"
value="<cfoutput><cfif mode EQ 'update'>#qEdit.KODE_CUSTOMER#</cfif></cfoutput>">

<br><br>

Tanggal<br>

<input
type="date"
name="TANGGAL_PEMBELIAN"
value="<cfoutput><cfif mode EQ 'update'>#dateFormat(qEdit.TANGGAL_PEMBELIAN,'yyyy-mm-dd')#</cfif></cfoutput>">

<br><br>

Jumlah<br>

<input
name="JUMLAH_PEMBELIAN"
value="<cfoutput><cfif mode EQ 'update'>#qEdit.JUMLAH_PEMBELIAN#</cfif></cfoutput>">

<br><br>

<input
type="submit"
name="btnSimpan"
value="Simpan">

</form>

<hr>

<table border="1" cellpadding="5">

<tr>
    <th>Kode</th>
    <th>Barang</th>
    <th>Customer</th>
    <th>Tanggal</th>
    <th>Jumlah</th>
    <th>Aksi</th>
</tr>

<cfoutput query="qPembelian">

<tr>

<td>#KODE_PEMBELIAN#</td>
<td>#KODE_BARANG#</td>
<td>#KODE_CUSTOMER#</td>
<td>#dateFormat(TANGGAL_PEMBELIAN,"dd-mm-yyyy")#</td>
<td>#JUMLAH_PEMBELIAN#</td>

<td>

<a href="pembelian_all.cfm?action=edit&kode=#KODE_PEMBELIAN#">
Edit
</a>

|

<a
href="pembelian_all.cfm?action=delete&kode=#KODE_PEMBELIAN#"
onclick="return confirm('Yakin ingin menghapus data #JSStringFormat(KODE_PEMBELIAN)# ?');">
Delete
</a>

</td>

</tr>

</cfoutput>

</table>

</body>

</html>
[root@teguhth-all ROOT]#

7. test login 

http://10.10.10.90:8888/pembelian_all.cfm
 


No comments:

Post a Comment

Popular Posts