if ($type == 'info') {
// 处理图片上传
function handleFileUpload($file, $prefix)
{
if (!$file || !isset($file['name']) || $file['error'] != UPLOAD_ERR_OK) {
return '';
}
$targetDir = $_SERVER['DOCUMENT_ROOT'] . "/TMP_Infobooth/Sources/InformationCard/";
$fileName = $prefix . '_' . time() . '_' . basename($file['name']);
$targetFile = $targetDir . $fileName;
if (move_uploaded_file($file['tmp_name'], $targetFile)) {
return "https://digimktgsolution.com/TMP_Infobooth/Sources/InformationCard/" . $fileName;
}
return '';
}
// 处理新增
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_info'])) {
$type = $_POST['type'];
$tcName = $_POST['tc_name'];
$scName = $_POST['sc_name'];
$engName = $_POST['eng_name'];
$tcUrl = handleFileUpload($_FILES['tc_url'], "tc");
$scUrl = handleFileUpload($_FILES['sc_url'], "sc");
$engUrl = handleFileUpload($_FILES['eng_url'], "eng");
$stmt = $conn->prepare("INSERT INTO TMPinfobooth_InformationCard
(Type, TCName, TCUrl, SCName, SCUrl, ENGName, ENGUrl)
VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssss", $type, $tcName, $tcUrl, $scName, $scUrl, $engName, $engUrl);
$stmt->execute();
$stmt->close();
header("Location: ?type=info");
exit;
}
// 处理更新
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update_info'])) {
$id = $_POST['id'];
$type = $_POST['type'];
$tcName = $_POST['tc_name'];
$scName = $_POST['sc_name'];
$engName = $_POST['eng_name'];
// 获取当前数据
$current = $conn->query("SELECT * FROM TMPinfobooth_InformationCard WHERE ID = $id")->fetch_assoc();
// 处理文件上传,保留现有URL如果没有新文件上传
$tcUrl = !empty($_FILES['tc_url']['name']) ? handleFileUpload($_FILES['tc_url'], "tc") : $current['TCUrl'];
$scUrl = !empty($_FILES['sc_url']['name']) ? handleFileUpload($_FILES['sc_url'], "sc") : $current['SCUrl'];
$engUrl = !empty($_FILES['eng_url']['name']) ? handleFileUpload($_FILES['eng_url'], "eng") : $current['ENGUrl'];
$stmt = $conn->prepare("UPDATE TMPinfobooth_InformationCard
SET Type=?, TCName=?, TCUrl=?, SCName=?, SCUrl=?, ENGName=?, ENGUrl=?
WHERE ID=?");
$stmt->bind_param("sssssssi", $type, $tcName, $tcUrl, $scName, $scUrl, $engName, $engUrl, $id);
$stmt->execute();
$stmt->close();
header("Location: ?type=info");
exit;
}
// 处理删除
if (isset($_GET['delete_info'])) {
$id = $_GET['delete_info'];
$stmt = $conn->prepare("DELETE FROM TMPinfobooth_InformationCard WHERE ID=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->close();
header("Location: ?type=info");
exit;
}
// 获取所有资讯并按OrderNum排序
$result = $conn->query("SELECT * FROM TMPinfobooth_InformationCard ORDER BY OrderNum ASC");
?>
<!DOCTYPE html>
<html lang="zh-HK">
<head>
<meta charset="UTF-8">
<meta property="og:image" content="https://digimktgsolution.com/TMP_Infobooth/Sources/logo.jpg">
<meta property="og:image:type" content="image/jpg">
<meta property="og:image:width" content="510">
<meta property="og:image:height" content="510">
<meta property="og:title" content="TMP_Infobooth CMS" />
<meta property="og:description" content="TMP_Infobooth CMS" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>爬行动物资讯管理</title>
<style>
body {
font-family: Arial, sans-serif;
}
.container {
width: 95%;
margin: 20px auto;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #009879;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
.image-preview {
max-width: 100px;
max-height: 100px;
}
.btn {
padding: 5px 10px;
text-decoration: none;
border-radius: 3px;
cursor: pointer;
border: none;
}
.edit-btn {
background: #4CAF50;
color: white;
}
.delete-btn {
background: #f44336;
color: white;
}
.add-btn {
background: #2196F3;
color: white;
padding: 10px 15px;
display: inline-block;
margin-bottom: 20px;
text-decoration: none;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 1000;
overflow: auto;
}
.modal-content {
background: white;
margin: 5% auto;
padding: 20px;
width: 80%;
max-width: 700px;
border-radius: 5px;
position: relative;
}
.close {
position: absolute;
top: 10px;
right: 15px;
font-size: 24px;
cursor: pointer;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
select {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.form-row {
display: flex;
gap: 15px;
margin-bottom: 15px;
}
.form-col {
flex: 1;
}
.image-upload {
border: 1px dashed #ccc;
padding: 10px;
text-align: center;
margin-top: 5px;
}
.current-image {
max-width: 100px;
max-height: 100px;
margin-top: 10px;
}
.btn-container {
display: flex;
gap: 10px;
margin-top: 20px;
}
.btn-container button {
padding: 8px 15px;
cursor: pointer;
}
</style>
</head>
<body>
<a href="?type=main" style="display:inline-block; margin:10px;">← 返回主菜单</a>
<div class="container">
<h1>爬行动物资讯管理</h1>
<a href="#" class="add-btn" onclick="openAddModal()">新增资讯</a>
<table>
<tr>
<th>ID</th>
<th>类型 (Type)</th>
<th>繁体名称 (TCName)</th>
<th>繁体图片 (TCUrl)</th>
<th>简体名称 (SCName)</th>
<th>简体图片 (SCUrl)</th>
<th>英文名称 (ENGName)</th>
<th>英文图片 (ENGUrl)</th>
<th>更新时间</th>
<th>操作</th>
</tr>
<?php while ($row = $result->fetch_assoc()): ?>
<tr>
<td><?= htmlspecialchars($row['ID']) ?></td>
<td><?= htmlspecialchars($row['Type']) ?></td>
<td><?= htmlspecialchars($row['TCName']) ?></td>
<td>
<?php if (!empty($row['TCUrl'])): ?>
<img src="<?= htmlspecialchars($row['TCUrl']) ?>" class="image-preview">
<?php else: ?>
-
<?php endif; ?>
</td>
<td><?= htmlspecialchars($row['SCName']) ?></td>
<td>
<?php if (!empty($row['SCUrl'])): ?>
<img src="<?= htmlspecialchars($row['SCUrl']) ?>" class="image-preview">
<?php else: ?>
-
<?php endif; ?>
</td>
<td><?= htmlspecialchars($row['ENGName']) ?></td>
<td>
<?php if (!empty($row['ENGUrl'])): ?>
<img src="<?= htmlspecialchars($row['ENGUrl']) ?>" class="image-preview">
<?php else: ?>
-
<?php endif; ?>
</td>
<td><?= htmlspecialchars($row['UpdateTime']) ?></td>
<td>
<button class="btn edit-btn" onclick="openEditModal(
<?= $row['ID'] ?>,
'<?= addslashes($row['Type']) ?>',
'<?= addslashes($row['TCName']) ?>',
'<?= addslashes($row['TCUrl']) ?>',
'<?= addslashes($row['SCName']) ?>',
'<?= addslashes($row['SCUrl']) ?>',
'<?= addslashes($row['ENGName']) ?>',
'<?= addslashes($row['ENGUrl']) ?>'
)">编辑</button>
<a href="?type=info&delete_info=<?= $row['ID'] ?>" class="btn delete-btn"
onclick="return confirm('确定删除?')">删除</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<!-- 编辑弹窗 -->
<div id="editModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeModal('editModal')">×</span>
<h2>编辑资讯</h2>
<form id="editForm" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" id="editId">
<div class="form-row">
<div class="form-col">
<div class="form-group">
<label>类型 (Type):</label>
<input type="text" name="type" id="editType" required>
</div>
</div>
</div>
<div class="form-row">
<div class="form-col">
<div class="form-group">
<label>繁体名称 (TCName):</label>
<input type="text" name="tc_name" id="editTCName" required>
</div>
</div>
<div class="form-col">
<div class="form-group">
<label>简体名称 (SCName):</label>
<input type="text" name="sc_name" id="editSCName" required>
</div>
</div>
<div class="form-col">
<div class="form-group">
<label>英文名称 (ENGName):</label>
<input type="text" name="eng_name" id="editENGName" required>
</div>
</div>
</div>
<div class="form-row">
<div class="form-col">
<div class="form-group">
<label>繁体图片 (TCUrl):</label>
<div class="image-upload">
<input type="file" name="tc_url" accept="image/*">
<div id="editTCImagePreview"></div>
</div>
</div>
</div>
<div class="form-col">
<div class="form-group">
<label>简体图片 (SCUrl):</label>
<div class="image-upload">
<input type="file" name="sc_url" accept="image/*">
<div id="editSCImagePreview"></div>
</div>
</div>
</div>
<div class="form-col">
<div class="form-group">
<label>英文图片 (ENGUrl):</label>
<div class="image-upload">
<input type="file" name="eng_url" accept="image/*">
<div id="editENGImagePreview"></div>
</div>
</div>
</div>
</div>
<div class="btn-container">
<button type="submit" name="update_info">更新</button>
<button type="button" onclick="closeModal('editModal')">取消</button>
</div>
</form>
</div>
</div>
<!-- 新增弹窗 -->
<div id="addModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeModal('addModal')">×</span>
<h2>新增资讯</h2>
<form method="post" enctype="multipart/form-data">
<div class="form-row">
<div class="form-col">
<div class="form-group">
<label>类型 (Type):</label>
<input type="text" name="type" required>
</div>
</div>
</div>
<div class="form-row">
<div class="form-col">
<div class="form-group">
<label>繁体名称 (TCName):</label>
<input type="text" name="tc_name" required>
</div>
</div>
<div class="form-col">
<div class="form-group">
<label>简体名称 (SCName):</label>
<input type="text" name="sc_name" required>
</div>
</div>
<div class="form-col">
<div class="form-group">
<label>英文名称 (ENGName):</label>
<input type="text" name="eng_name" required>
</div>
</div>
</div>
<div class="form-row">
<div class="form-col">
<div class="form-group">
<label>繁体图片 (TCUrl):</label>
<div class="image-upload">
<input type="file" name="tc_url" accept="image/*" required>
</div>
</div>
</div>
<div class="form-col">
<div class="form-group">
<label>简体图片 (SCUrl):</label>
<div class="image-upload">
<input type="file" name="sc_url" accept="image/*" required>
</div>
</div>
</div>
<div class="form-col">
<div class="form-group">
<label>英文图片 (ENGUrl):</label>
<div class="image-upload">
<input type="file" name="eng_url" accept="image/*" required>
</div>
</div>
</div>
</div>
<div class="btn-container">
<button type="submit" name="add_info">添加</button>
<button type="button" onclick="closeModal('addModal')">取消</button>
</div>
</form>
</div>
</div>
<script>
function openEditModal(id, type, tcName, tcUrl, scName, scUrl, engName, engUrl) {
document.getElementById('editId').value = id;
document.getElementById('editType').value = type;
document.getElementById('editTCName').value = tcName;
document.getElementById('editSCName').value = scName;
document.getElementById('editENGName').value = engName;
// 显示预览图
document.getElementById('editTCImagePreview').innerHTML = tcUrl ?
`<p>当前图片:</p><img src="${tcUrl}" class="current-image">` :
'<p>无图片</p>';
document.getElementById('editSCImagePreview').innerHTML = scUrl ?
`<p>当前图片:</p><img src="${scUrl}" class="current-image">` :
'<p>无图片</p>';
document.getElementById('editENGImagePreview').innerHTML = engUrl ?
`<p>当前图片:</p><img src="${engUrl}" class="current-image">` :
'<p>无图片</p>';
document.getElementById('editModal').style.display = 'block';
}
function openAddModal() {
document.getElementById('addModal').style.display = 'block';
}
function closeModal(modalId) {
document.getElementById(modalId).style.display = 'none';
}
// 点击模态框外部关闭
window.onclick = function (event) {
if (event.target.className === 'modal') {
closeModal(event.target.id);
}
}
// 点击关闭按钮
document.querySelectorAll('.close').forEach(closeBtn => {
closeBtn.addEventListener('click', function () {
const modal = this.closest('.modal');
modal.style.display = 'none';
});
});
</script>
</body>
</html>
<?php
exit;
}
使用以上修改后的最新资讯管理輸入框的驗證方法:validateInput和validateForm,同樣在爬行动物资讯管理裏面的新增和編輯裏面的輸入框進行繁體,簡體,英文的驗證
最新发布