Edit Customer Details

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Update customer details $stmt = $db->prepare("UPDATE customers SET name = :name, mobile = :mobile, email = :email WHERE customerId = :customerId"); $stmt->bindParam(':name', $_POST['name']); $stmt->bindParam(':mobile', $_POST['mobile']); $stmt->bindParam(':email', $_POST['email']); $stmt = $db->prepare("SELECT COUNT(*) FROM address WHERE customerId = :customerId"); $stmt->bindParam(':customerId', $_POST['customerId']); $stmt->execute(); $addressExists = $stmt->fetchColumn(); if ($addressExists) { $stmt = $db->prepare("UPDATE address SET city = :city, district = :district, state = :state, pinCode = :pin WHERE customerId = :customerId"); } else { $stmt = $db->prepare("INSERT INTO address (customerId, city, district, state, pinCode) VALUES (:customerId, :city, :district, :state, :pin)"); } $stmt->bindParam(':city', $_POST['city']); $stmt->bindParam(':district', $_POST['district']); $stmt->bindParam(':state', $_POST['state']); $stmt->bindParam(':pin', $_POST['pinCode']); $stmt->bindParam(':customerId', $_POST['customerId']); if ($stmt->execute()) { echo '
Customer details updated successfully.
'; } else { echo '
Error updating customer.
'; } } // Fetch customer data $stmt = $db->prepare("SELECT * FROM customers WHERE customerId = :customerId"); $stmt->bindParam(':customerId', $_GET['customerId']); $stmt->execute(); $customer = $stmt->fetch(PDO::FETCH_ASSOC); $stmtAddress = $db->prepare("SELECT * FROM address WHERE customerId = :customerId"); $stmtAddress->bindParam(':customerId', $_GET['customerId']); $stmtAddress->execute(); $customerAddress = $stmtAddress->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { echo '
Error: ' . $e->getMessage() . '
'; } ?>
Customer not found!