You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中DB常量重复定义报错及数据库查询函数运行问题求助

Hey there, let's walk through fixing your error and cleaning up some other potential issues in your code.

1. Root Cause of "Constant DB already defined" Error

Your getDBConnection() function in config.php runs define() for all your DB constants every time the function is called. Since you call this function twice (once in getDataToko() and once in getDataAkses()), PHP throws an error because it doesn't allow redefining existing constants.

2. Fix for the Constant Error

Move the constant definitions outside the getDBConnection() function so they only run once when the file is included, not every time the function executes. Here's the corrected config.php:

<?php 
// Define constants once at the top, outside the function
define('DB_SERVER', 'localhost'); 
define('DB_USERNAME', 'root'); 
define('DB_PASSWORD', ''); 
define('DB_DATABASE', 'dbcity'); 

function getDBConnection() { 
    $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE); 
    if(!$db) { 
        die("<script>alert('Gagal tersambung dengan database.')</script>"); 
    } 
    return $db; 
} 
?>

3. Other Critical Fixes in Your HTML/PHP Code

a. Undefined Variable in Buttons

In your table row loop, you're using $dataToko['id_ot'] for the Edit/Delete button data-id attribute—but $dataToko only exists in the earlier <select> loop. This will throw an "Undefined index" error. Replace it with $dataAkses['id_ot'] since that's the variable in scope here:

<button type="button" class="view-detail btn btn-success" data-id="<?= $dataAkses['id_ot'] ?>"> Edit </button>
<button type="button" class="view-delete btn btn-danger" data-id="<?= $dataAkses['id_ot'] ?>"> Delete </button>

b. Duplicate ID Attribute

You have id="deleteButton" inside the loop, which creates multiple elements with the same ID (invalid HTML). Remove the fixed ID or make it unique:

<button type="button" class="view-delete btn btn-danger" data-id="<?= $dataAkses['id_ot'] ?>" id="deleteButton-<?= $dataAkses['id_ha'] ?>"> Delete </button>

c. Repeated Includes Inside Loop

You're including hapus_toko.php and edit_toko.php inside the while loop, which means their code will be inserted once per row. This can cause duplicate function definitions or unexpected HTML. Move these includes to the top of your HTML page instead:

<?php 
include 'hapus_toko.php'; 
include 'edit_toko.php'; 
?>
<!-- Rest of your HTML code below -->

4. Optional Optimization (Database Connections)

Right now, you open and close a database connection for every function call. For better performance, you could reuse a single connection instead of creating new ones each time. Here's a quick way to adjust this:

// In config.php
function getDBConnection() {
    static $db; // Static variable retains its value between function calls
    if (!$db) {
        $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
        if(!$db) {
            die("<script>alert('Gagal tersambung dengan database.')</script>");
        }
    }
    return $db;
}

This way, the connection is created only once, even if you call getDBConnection() multiple times.

内容的提问来源于stack exchange,提问作者Ridho Wisnu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 16:14:03