diff --git a/README.md b/README.md index 944851b..b013ae4 100644 --- a/README.md +++ b/README.md @@ -193,11 +193,13 @@ carwash_order/ CREATE TABLE vip_customers ( id INT AUTO_INCREMENT PRIMARY KEY, customer_name VARCHAR(100) NOT NULL COMMENT '客户姓名', - phone VARCHAR(20) NOT NULL UNIQUE COMMENT '联系电话(唯一)', + phone VARCHAR(20) NOT NULL COMMENT '联系电话', car_model VARCHAR(50) COMMENT '车型', car_number VARCHAR(20) COMMENT '车牌号', email VARCHAR(100) COMMENT '邮箱地址', birthday DATE COMMENT '生日', + -- 复合唯一索引:确保手机号和车牌号组合唯一 + UNIQUE KEY idx_phone_car_number (phone, car_number) notes TEXT COMMENT '备注信息', is_active BOOLEAN DEFAULT TRUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, diff --git a/carwash_db.sql b/carwash_db.sql index c5124fe..9229641 100644 --- a/carwash_db.sql +++ b/carwash_db.sql @@ -6,7 +6,7 @@ USE carwash_booking; CREATE TABLE IF NOT EXISTS vip_customers ( id INT AUTO_INCREMENT PRIMARY KEY, customer_name VARCHAR(100) NOT NULL COMMENT '客户姓名', - phone VARCHAR(20) NOT NULL UNIQUE COMMENT '联系电话(唯一)', + phone VARCHAR(20) NOT NULL COMMENT '联系电话', car_model VARCHAR(50) COMMENT '车型', car_number VARCHAR(20) COMMENT '车牌号', email VARCHAR(100) COMMENT '邮箱地址', @@ -14,7 +14,9 @@ CREATE TABLE IF NOT EXISTS vip_customers ( notes TEXT COMMENT '备注信息', is_active BOOLEAN DEFAULT TRUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP NULL + updated_at TIMESTAMP NULL, + -- 添加phone和car_number的复合唯一索引,确保手机号+车牌号组合不重复 + UNIQUE INDEX idx_phone_car_number (phone, car_number) ); -- 创建套餐表 diff --git a/update_vip_index.sql b/update_vip_index.sql new file mode 100644 index 0000000..18830f1 --- /dev/null +++ b/update_vip_index.sql @@ -0,0 +1,11 @@ +-- 更新VIP客户表的索引结构 +USE carwash_booking; + +-- 1. 移除phone字段的UNIQUE约束(需要先删除原索引) +ALTER TABLE vip_customers DROP INDEX phone; + +-- 2. 添加phone和car_number的复合唯一索引 +ALTER TABLE vip_customers ADD UNIQUE INDEX idx_phone_car_number (phone, car_number); + +-- 3. 验证索引添加成功 +SHOW INDEX FROM vip_customers; \ No newline at end of file diff --git a/update_vip_table.php b/update_vip_table.php new file mode 100644 index 0000000..825f8c1 --- /dev/null +++ b/update_vip_table.php @@ -0,0 +1,59 @@ +"; + + // 1. 移除phone字段的UNIQUE约束 + echo "正在移除phone字段的UNIQUE约束...
"; + $stmt = $pdo->prepare("ALTER TABLE vip_customers DROP INDEX phone"); + $stmt->execute(); + echo "✓ phone字段UNIQUE约束已移除
"; + + // 2. 添加phone和car_number的复合唯一索引 + echo "正在添加phone+car_number的复合唯一索引...
"; + $stmt = $pdo->prepare("ALTER TABLE vip_customers ADD UNIQUE INDEX idx_phone_car_number (phone, car_number)"); + $stmt->execute(); + echo "✓ 复合唯一索引已添加
"; + + // 3. 验证索引 + echo "正在验证索引...
"; + $stmt = $pdo->prepare("SHOW INDEX FROM vip_customers"); + $stmt->execute(); + $indexes = $stmt->fetchAll(PDO::FETCH_ASSOC); + + echo "
✓ 索引列表:
"; + echo ""; + echo ""; + foreach ($indexes as $index) { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + echo "
TableNon_uniqueKey_nameColumn_name
{$index['Table']}{$index['Non_unique']}{$index['Key_name']}{$index['Column_name']}
"; + + echo "
✅ 数据库结构更新成功!"; + +} catch (PDOException $e) { + echo "
❌ 错误: " . $e->getMessage() . "
"; + + // 检查是否是因为索引不存在导致的错误 + if (strpos($e->getMessage(), "doesn't exist") !== false) { + echo "可能是因为索引已经不存在,尝试直接添加复合索引...
"; + try { + $stmt = $pdo->prepare("ALTER TABLE vip_customers ADD UNIQUE INDEX idx_phone_car_number (phone, car_number)"); + $stmt->execute(); + echo "✓ 复合唯一索引已添加
"; + } catch (PDOException $e2) { + echo "
❌ 添加索引失败: " . $e2->getMessage() . "
"; + } + } +} finally { + // 关闭连接 + $pdo = null; +} +?> \ No newline at end of file diff --git a/vip.php b/vip.php index 50909a8..13ca0d4 100644 --- a/vip.php +++ b/vip.php @@ -24,20 +24,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { throw new Exception('请填写客户姓名和联系电话'); } - // 检查VIP客户是否已存在 - $stmt = $pdo->prepare("SELECT id FROM vip_customers WHERE phone = ?"); - $stmt->execute([$phone]); + // 检查VIP客户是否已存在(基于手机号和车牌号组合) + $stmt = $pdo->prepare("SELECT id FROM vip_customers WHERE phone = ? AND car_number = ?"); + $stmt->execute([$phone, $car_number]); if ($stmt->fetch()) { - throw new Exception('该手机号码已经是VIP客户'); + throw new Exception('该手机号和车牌号组合已经是VIP客户'); } // 插入VIP客户 $stmt = $pdo->prepare("INSERT INTO vip_customers (customer_name, phone, car_model, car_number, email, birthday, notes) VALUES (?, ?, ?, ?, ?, ?, ?)"); - $stmt->execute([$customer_name, $phone, $car_model, $car_number, $email, $birthday, $notes]); - - $success_message = "VIP客户录入成功!"; + try { + $stmt->execute([$customer_name, $phone, $car_model, $car_number, $email, $birthday, $notes]); + $success_message = "VIP客户录入成功!"; + } catch (PDOException $e) { + // 捕获唯一约束错误 + if ($e->errorInfo[1] == 1062) { // MySQL唯一键约束错误码 + throw new Exception('该手机号和车牌号组合已经是VIP客户'); + } + throw $e; // 重新抛出其他类型的异常 + } } elseif ($action === 'delete_vip') { $id = (int)$_POST['vip_id'];