-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_code.php
More file actions
executable file
·85 lines (74 loc) · 2.99 KB
/
Copy pathsave_code.php
File metadata and controls
executable file
·85 lines (74 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
header('Content-Type: application/json');
// 连接数据库
$servername = "localhost";
$username = "material_codes";
$password = "yrmkks2PNNbnxdyR";
$dbname = "material_codes";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die(json_encode(['status' => 'error', 'message' => '数据库连接失败: ' . $conn->connect_error]));
}
// 获取前端发送的数据
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['type']) || !isset($data['sub_type']) || !isset($data['properties']) || !isset($data['code'])) {
echo json_encode(['status' => 'error', 'message' => '缺少必要的数据字段']);
exit;
}
$type = $data['type'];
$sub_type = $data['sub_type'];
$properties = json_encode($data['properties']);
$code = $data['code'];
// 检查是否存在相同的物料信息
$checkSql = "SELECT * FROM material_codes WHERE type = ? AND sub_type = ? AND properties = ?";
$stmtCheck = $conn->prepare($checkSql);
$stmtCheck->bind_param("sss", $type, $sub_type, $properties);
$stmtCheck->execute();
$resultCheck = $stmtCheck->get_result();
if ($resultCheck->num_rows > 0) {
echo json_encode(['status' => 'error', 'message' => '物料信息已存在,保存失败']);
exit;
}
// 检查编码是否已存在
$checkCodeSql = "SELECT * FROM material_codes WHERE code = ?";
$stmtCheckCode = $conn->prepare($checkCodeSql);
$stmtCheckCode->bind_param("s", $code);
$stmtCheckCode->execute();
$resultCheckCode = $stmtCheckCode->get_result();
if ($resultCheckCode->num_rows > 0) {
// 如果编码已存在,更新记录
$updateSql = "UPDATE material_codes SET type = ?, sub_type = ?, properties = ? WHERE code = ?";
$stmtUpdate = $conn->prepare($updateSql);
if ($stmtUpdate->bind_param("ssss", $type, $sub_type, $properties, $code) && $stmtUpdate->execute()) {
$response = json_encode(['status' => 'success', 'message' => '编码信息已更新']);
error_log('Response: ' . $response); // 添加这行日志
echo $response;
} else {
echo json_encode(['status' => 'error', 'message' => '更新编码信息时出错: ' . $stmtUpdate->error]);
}
} else {
// 如果编码不存在,插入新记录
$insertSql = "INSERT INTO material_codes (type, sub_type, properties, code) VALUES (?, ?, ?, ?)";
$stmtInsert = $conn->prepare($insertSql);
if ($stmtInsert->bind_param("ssss", $type, $sub_type, $properties, $code) && $stmtInsert->execute()) {
$response = json_encode(['status' => 'success', 'message' => '编码已成功保存']);
error_log('Response: ' . $response); // 添加这行日志
echo $response;
} else {
echo json_encode(['status' => 'error', 'message' => '执行插入时出错: ' . $stmtInsert->error]);
}
}
if (isset($stmtCheck)) {
$stmtCheck->close();
}
if (isset($stmtCheckCode)) {
$stmtCheckCode->close();
}
if (isset($stmtUpdate)) {
$stmtUpdate->close();
}
if (isset($stmtInsert)) {
$stmtInsert->close();
}
$conn->close();
?>