codota / TabNine

AI Code Completions
https://tabnine.com
MIT License
10.63k stars 498 forks source link

Data not saving to the data base #660

Open kvijayarajan opened 3 days ago

kvijayarajan commented 3 days ago

please complete the following information:

Issue Details:

<?php // Enable error reporting for debugging ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

error_log(print_r($_POST, true)); // This will log the contents of the POST request

$servername = "localhost"; // Change if necessary $username = "root"; // Change if necessary $password = ""; // Change if necessary $dbname = "mobile_pos"; // Change if necessary

// Connect to the database $conn = new mysqli($servername, $username, $password, $dbname);

// Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }

// Generate invoice number $invoice_number = "INV-" . uniqid(); // Example format INV-

// Fetch customer names from the database $customers = []; $sql = "SELECT id AS customer_id, name AS customer_name FROM customers"; $result = $conn->query($sql);

if ($result && $result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $customers[] = $row; } } else { echo "

No customers found.

"; }

// Fetch items from the stock table $items = []; $sql = "SELECT barcode, item_name, cost_price FROM stock"; // Use cost_price from the stock table $result = $conn->query($sql);

if ($result && $result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $items[] = $row; } } // Generate invoice number function generateInvoiceNumber($conn) { $sql = "SELECT invoice_number FROM invoices ORDER BY id DESC LIMIT 1"; $result = $conn->query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); $lastInvoiceNumber = $row['invoice_number']; $lastNumber = (int)substr($lastInvoiceNumber, 4); $nextNumber = $lastNumber + 1;

    if ($nextNumber > 999999) {
        $nextNumber = 1; // Reset to INV-000001 after reaching INV-999999
    }
    return "INV-" . str_pad($nextNumber, 6, "0", STR_PAD_LEFT);
} else {
    return "INV-000001"; // First invoice
}

}

$invoice_number = generateInvoiceNumber($conn);

// Fetch payment methods from the database $payment_methods = []; $sql = "SELECT id AS payment_id, method_name FROM payment_methods"; $result = $conn->query($sql);

if ($result && $result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $payment_methods[] = $row; } } else { echo "

No payment methods found.

"; }

// Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['customer'])) { $customer_id = $_POST['customer']; // $payment_details = json_decode($_POST['payment_details'], true); error_log(print_r($_POST, true)); // This will log the contents of the POST request

    // Validate customer selection
    if (empty($customer_id) || empty($payment_details)) {
        $error = "Please fill in all fields.";
    } else {
        // Fetch the customer's name based on the customer_id
        $customer_sql = "SELECT name FROM customers WHERE id = ?";
        $stmt = $conn->prepare($customer_sql);
        $stmt->bind_param("i", $customer_id);
        $stmt->execute();
        $result = $stmt->get_result();
        $customer = $result->fetch_assoc();
        $client_name = $customer['name'];

        // Insert into invoices
        $invoice_sql = "INSERT INTO invoices (invoice_number, customer_id, client_name, date) VALUES (?, ?, ?, NOW())";
        $stmt = $conn->prepare($invoice_sql);
        $stmt->bind_param("sis", $invoice_number, $customer_id, $client_name);
        if (!$stmt->execute()) {
            $error = "Error inserting invoice: " . $stmt->error;
        } else {
            // Get the last inserted invoice ID
            $invoice_id = $conn->insert_id;

            // Process invoice items
            for ($i = 0; $i < count($_POST['barcode']); $i++) {
                $barcode = $_POST['barcode'][$i];
                $item_name = $_POST['item_name'][$i];
                $quantity = $_POST['quantity'][$i];
                $item_price = $_POST['item_price'][$i];
                $discount = $_POST['discount'][$i];

                // Calculate line total
                $line_total = ($item_price - $discount) * $quantity;

                // Insert invoice items into the database
                $invoice_item_sql = "INSERT INTO invoice_items (invoice_id, barcode, item_name, item_quantity, item_price, discount, line_total) VALUES (?, ?, ?, ?, ?, ?, ?)";
                $stmt = $conn->prepare($invoice_item_sql);
                $stmt->bind_param("issidsd", $invoice_id, $barcode, $item_name, $quantity, $item_price, $discount, $line_total);
                if (!$stmt->execute()) {
                    $error = "Error inserting invoice item: " . $stmt->error;
                }
            }

            // Handle payment details
            foreach ($payment_details as $payment) {
                $payment_method = $payment['payment_method'];
                $amount = $payment['amount'];

                // Insert payment details into the database (assuming a payment table exists)
                $payment_sql = "INSERT INTO payments (invoice_id, payment_method, amount) VALUES (?, ?, ?)";
                $stmt = $conn->prepare($payment_sql);
                $stmt->bind_param("isd", $invoice_id, $payment_method, $amount);
                $stmt->execute();
            }

            // Clear form fields after submission
            $_POST = [];
        }
    }
}

// Handle AJAX request to fetch customer details
if (isset($_POST['fetch_customer_details'])) {
    $customer_id = $_POST['customer_id'];
    $sql = "SELECT name, address, telephone, email FROM customers WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $customer_id);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $customer_details = $result->fetch_assoc();
        echo json_encode($customer_details);
    } else {
        echo json_encode(['name' => '', 'address' => '', 'telephone' => '', 'email' => '']);
    }
    exit(); // Exit to prevent further output
}

// Handle AJAX request to fetch item name and price based on barcode
if (isset($_POST['fetch_item_details'])) {
    $barcode = $_POST['barcode'];
    $sql = "SELECT item_name, cost_price FROM stock WHERE barcode = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $barcode);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $item = $result->fetch_assoc();
        echo json_encode($item);
    } else {
        echo json_encode(['item_name' => '', 'cost_price' => '']);
    }
    exit(); // Exit to prevent further output
}

}

// Close database connection $conn->close(); ?>

<!DOCTYPE html>

Invoice Management System

Invoice Management System

Invoice Number:

Customer Details

Customer Name:

Address:

Telephone:

Email:

Invoice Items

Item Item Name Quantity Item Price Discount Line Total Action

Total

0.00
0.00
0.00

Payment Methods

gz#36275

(related to Zendesk ticket #36275)

TzufTabnine commented 2 days ago

Hi @kvijayarajan, What is the issue here?