Insert and Update to database from custom file

i was working on this custom file (unchecked the include common files)

<?php

namespace PHPMaker2024\grams1;

use Psr\Container\ContainerInterface;
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Dflydev\DotAccessData\Data;

global $RELATIVE_PATH;

// --- Load Essentials ---
require_once "vendor/autoload.php";
require_once "src/constants.php";
require_once "src/config.php";
require_once "src/phpfn.php";
require_once "src/userfn.php";

// --- Initialize ConfigData ---
global $ConfigData;
$ConfigData = new Data($CONFIG);

// --- Build Container if Missing ---
if (!isset($GLOBALS["Container"]) || !($GLOBALS["Container"] instanceof ContainerInterface)) {
    $containerBuilder = new ContainerBuilder();
    $containerBuilder->useAttributes(true);
    if (IsProduction() && Config("COMPILE_CONTAINER") && !IsRemote(Config("UPLOAD_DEST_PATH"))) {
        $cacheFolder = Config("CACHE_FOLDER");
        if (CreateFolder($cacheFolder)) {
            $containerBuilder->enableCompilation($cacheFolder);
        }
    }
    $containerBuilder->addDefinitions("src/definitions.php");
    $GLOBALS["Container"] = $containerBuilder->build();
}

// --- Create Slim App if Not Exists ---
if (!isset($GLOBALS["App"])) {
    AppFactory::setContainer($GLOBALS["Container"]);
    $app = AppFactory::create();
    $GLOBALS["App"] = $app;
    $app->setBasePath(BasePath());
    $app->addRoutingMiddleware();
    \PHPMaker2024\grams1\RouteAttributes::registerRoutes($app);
    (require_once "src/routes.php")($app);
}

// --- Server Request ---
if (!isset($GLOBALS["Request"])) {
    $GLOBALS["Request"] = ServerRequestCreatorFactory::create()->createServerRequestFromGlobals();
}

// --- Language Initialization ---
global $Language;
if (!isset($Language)) {
    try {
        $Language = $GLOBALS["Container"]->get("language");
    } catch (\Throwable $e) {
        error_log("ERROR loading Language: " . $e->getMessage());
    }
}

// --- Session & Login Status ---
SetupLoginStatus();
SetClientVar("login", LoginStatus());

// --- Input Validation ---
$doc_id = (int) Get("fk_doc_id");
$user_id = CurrentUserID();

if (!$doc_id || !$user_id) {
    echo '<script>alert("Invalid request. Document ID or User ID is missing."); window.history.back();</script>';
    exit;
}

// --- Transaction-Safe DB Logic ---
$conn = Conn();
$conn->beginTransaction();

try {
    $stmt1 = $conn->prepare("UPDATE tbl_grievance SET alpha = 'encoder' WHERE doc_id = ?");
    $stmt1->bindValue(1, $doc_id, \PDO::PARAM_INT);
    $stmt1->execute();

    $stmt2 = $conn->prepare("INSERT INTO forwarded2_encoder (doc_id, date_forwarded, forwarded_by) VALUES (?, NOW(), ?)");
    $stmt2->bindValue(1, $doc_id, \PDO::PARAM_INT);
    $stmt2->bindValue(2, $user_id, \PDO::PARAM_STR);
    $stmt2->execute();

    $conn->commit();

    echo '<script>alert("Document forwarded successfully."); window.location.href = "tbl_grievance_list";</script>';
    exit;

} catch (\Throwable $e) {
    $conn->rollBack();
    $msg = addslashes($e->getMessage());
    error_log("DB Error: " . $e->getMessage());
    echo "<script>alert('Error occurred: {$msg}'); window.history.back();</script>";
    exit;
}

and i have this persistent error

Fatal error : Cannot declare class PHPMaker2024\grams1\Language, because the name is already in use in C:\xampp\htdocs\grams\src\Language.php on line 13

any work around to this?

Please do not use this out-dated approach anymore. If you keep using it, you’ll definitely need to update it every time there’s a new version.

You may try Route_Action server event or Custom File.

1 Like

i have question on route_action

1.is the global $conn available in route_action?
2. or do i configure PDO connection for the route?

You may use global function Conn() to get the connection.