38471-vm/test_last_insert.php
2026-02-25 09:58:14 +00:00

22 lines
950 B
PHP

<?php
require 'db/config.php';
$db = db();
try {
$db->exec("RENAME TABLE invoices TO _invoices");
$db->exec("CREATE FUNCTION current_outlet_id() RETURNS INT DETERMINISTIC RETURN @session_outlet_id");
$db->exec("CREATE VIEW invoices AS SELECT * FROM _invoices WHERE outlet_id = current_outlet_id() OR current_outlet_id() IS NULL");
$db->exec("SET @session_outlet_id = 1");
$db->exec("INSERT INTO invoices (customer_id, invoice_date, status, total_amount, vat_amount, total_with_vat, paid_amount) VALUES (1, '2026-01-01', 'unpaid', 100, 5, 105, 0)");
$id = $db->lastInsertId();
echo "Last Insert ID: " . $id . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
$db->exec("DROP VIEW IF EXISTS invoices");
$db->exec("RENAME TABLE _invoices TO invoices");
$db->exec("DELETE FROM invoices WHERE customer_id=1 AND total_amount=100");
$db->exec("DROP FUNCTION IF EXISTS current_outlet_id");