<?php
// Test with explicit bind
header('Content-Type: application/json');

try {
    $conn = new mysqli('localhost', 'zemaraim', '8wwxk]-X3Kx]CJ]5', 'zemaraim_interior');
    if ($conn->connect_error) throw new Exception("DB error");
    
    $stmt = $conn->prepare("INSERT INTO orders (
        user_id, first_name, last_name, email, phone,
        billing_country, billing_address, billing_city, billing_postcode,
        shipping_country, shipping_address, shipping_city, shipping_postcode,
        company, order_notes, payment_method, subtotal, tax, shipping, total, status
    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    
    $user_id = 6;
    $fn = "John";
    $ln = "Doe";
    $em = "j@test.com";
    $ph = "0300123";
    $bc = "Pakistan";
    $ba = "123 St";
    $bci = "Khi";
    $bp = "75000";
    $sc = "Pakistan";
    $sa = "456 Ave";
    $sci = "Lhr";
    $sp = "54000";
    $co = "Co";
    $on = "Note";
    $pm = "card";
    $sub = 5000.00;
    $tx = 500.00;
    $sh = 500.00;
    $tot = 6000.00;
    $st = "pending";
    
    // Type string: i + (15 s) + (4 d) + s = isssssssssssssssdddds (21 chars)
    $result = $stmt->bind_param("isssssssssssssssdddds",
        $user_id, $fn, $ln, $em, $ph, $bc, $ba, $bci, $bp, $sc, $sa, $sci, $sp, $co, $on, $pm,
        $sub, $tx, $sh, $tot, $st
    );
    
    if (!$result) throw new Exception("Bind: " . $stmt->error);
    if (!$stmt->execute()) throw new Exception("Exec: " . $stmt->error);
    
    echo json_encode(['success' => true, 'id' => $conn->insert_id]);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
?>
