1
0
Fork 0
This commit is contained in:
Chenx221 2024-02-10 16:30:45 +08:00
parent ba05ffb9da
commit df87ab4e13
6 changed files with 283 additions and 7 deletions

View File

@ -110,12 +110,13 @@ echo '</table>';
$result->free();
echo <<<EOL
<script>
//fix
const table = document.getElementById('bookTable');
table.addEventListener('click', function(event) {
const target = event.target;
if (target.classList.contains('editBtn')) { // 修改
const cells = target.parentNode.parentNode.cells;
for (let i = 1; i < cells.length - 1; i++) {
for (let i = 1; i < cells.length - 2; i++) {
const text = cells[i].innerText;
cells[i].innerText = '';
const input = document.createElement('input');
@ -129,11 +130,11 @@ echo <<<EOL
} else if (target.classList.contains('submitBtn')) { // 提交
const cells = target.parentNode.parentNode.cells;
const data = {
id: cells[0].innerText,
bookname: cells[1].firstChild.value,
price: cells[2].firstChild.value,
f_time: cells[3].firstChild.value,
type: cells[4].firstChild.value,
id: cells[1].childNodes[0].value,
bookname: cells[2].firstChild.value,
price: cells[3].firstChild.value,
f_time: cells[4].firstChild.value,
type: cells[5].firstChild.value,
update: true
};
fetch('api/18.test3.api.php', {
@ -148,7 +149,7 @@ echo <<<EOL
location.reload();
});
}else if (target.classList.contains('deleteBtn')) { // 删除
const id = target.parentNode.parentNode.cells[0].innerText;
const id = target.parentNode.parentNode.cells[1].innerText;
fetch('api/18.test3.api.php', {
method: 'POST',
headers: {

View File

@ -1,4 +1,5 @@
<?php
//work1
include_once 'config/pdo_connect.php';
/** @var PDO $pdo */
try {

View File

@ -1,4 +1,5 @@
<?php
//work1
include_once 'config/pdo_connect.php';
/** @var PDO $pdo */

198
19.work3.result.php Normal file
View File

@ -0,0 +1,198 @@
<?php
include_once 'config/pdo_connect_db18.php';
/** @var PDO $pdo */
/*
* 使用pdo代替mysqli
*/
echo '<style>
#bookTable {
border-collapse: collapse;
width: 80%;
text-align: center;
margin: auto;
font-family: Arial, sans-serif;
}
#bookTable th, #bookTable td {
border: 1px solid black;
padding: 10px;
}
#bookTable th {
background-color: #f2f2f2;
}
#bookTable tr:nth-child(even) {
background-color: #f2f2f2;
}
#bookTable tr:hover {
background-color: #ddd;
}
.editBtn, .deleteBtn, .batDelBtn {
padding: 5px 10px;
text-decoration: none;
font-size: 12px;
margin: 2px 2px;
cursor: pointer;
border: none;
}
.editBtn {
background-color: #4CAF50;
color: white;
}
.deleteBtn {
background-color: #f44336;
color: white;
}
.batDelBtn {
background-color: #008CBA;
color: white;
}
#announcement {
background-color: #f9f9f9;
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 20px;
font-size: 18px;
text-align: center;
}
</style>';
//公告栏
echo '<div id="announcement"><h3>公告</h3>';
$sql = "SELECT title FROM tb_affiche ORDER BY createtime DESC LIMIT 3";
$stmt = $pdo->query($sql);
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
$title = $row['title'];
if (mb_strlen($title, 'UTF-8') > 15) {
$title = mb_substr($title, 0, 15, 'UTF-8') . '...';
}
echo '<p>' . $title . '</p>';
}
} else {
echo '<p>暂无公告</p>';
}
echo '</div>';
$records_per_page = 10;
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($page - 1) * $records_per_page;
$sql = "select * from tb_demo02 limit $offset, $records_per_page";
$stmt = $pdo->query($sql);
echo '<table style="border-collapse: collapse;width: 80%;text-align: center;" id="bookTable">';
echo '<tr style="border: 1px solid black"><td>全选<input type="checkbox" name="batchSelect"></td><th>id</th><th>name</th><th>price</th><th>date</th><th>type</th><th>operation</th><th>operation2</th></tr>';
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
echo '<tr>';
echo '<td><input type="checkbox" name="id" value="' . $row['id'] . '"></td>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['bookname'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['f_time'] . '</td>';
echo '<td>' . $row['type'] . '</td>';
echo '<td><button class="editBtn">修改</button></td>';
echo '<td><button class="deleteBtn">删除</button></td>';
echo '</tr>';
}
}
echo '<tr><td><button class="batDelBtn">批量删除</button></td>';
$sql = 'select count(*) as count from tb_demo02';
$stmt = $pdo->query($sql);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_records = $row['count'];
$total_pages = ceil($total_records / $records_per_page);
echo '<td colspan="7">';
for ($i = 1; $i <= $total_pages; $i++) {
echo '<a href="19.work3.result.php?page=' . $i . '">' . $i . '</a> ';
}
echo '</td></tr>';
echo '</table>';
$stmt->closeCursor();
echo <<<EOL
<script>
// 之前添加的选框破坏了单个修改的功能
// 现已修复
const table = document.getElementById('bookTable');
table.addEventListener('click', function(event) {
const target = event.target;
if (target.classList.contains('editBtn')) { // 修改
const cells = target.parentNode.parentNode.cells;
for (let i = 1; i < cells.length - 2; i++) {
const text = cells[i].innerText;
cells[i].innerText = '';
const input = document.createElement('input');
input.type = 'text';
input.value = text;
cells[i].appendChild(input);
}
target.innerText = '提交';
target.classList.remove('editBtn');
target.classList.add('submitBtn');
} else if (target.classList.contains('submitBtn')) { // 提交
const cells = target.parentNode.parentNode.cells;
const data = {
id: cells[1].childNodes[0].value,
bookname: cells[2].firstChild.value,
price: cells[3].firstChild.value,
f_time: cells[4].firstChild.value,
type: cells[5].firstChild.value,
update: true
};
fetch('api/19.work3.api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(response => response.json())
.then(data => {
alert(data.message);
location.reload();
});
}else if (target.classList.contains('deleteBtn')) { // 删除
const id = target.parentNode.parentNode.cells[1].innerText;
fetch('api/19.work3.api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({id: id, delete: true})
}).then(response => response.json())
.then(data => {
alert(data.message);
location.reload();
});
}else if (target.name === 'batchSelect') { // 全选
const checkboxes = document.getElementsByName('id');
for (let i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = target.checked;
}
}else if (target.classList.contains('batDelBtn')) { // 批量删除
const checkboxes = document.getElementsByName('id');
const ids = [];
for (let i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
ids.push(checkboxes[i].value);
}
}
fetch('api/19.work3.api.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({id: ids, batDelete: true})
}).then(response => response.json())
.then(data => {
alert(data.message);
location.reload();
});
}
});
</script>
EOL;

62
api/19.work3.api.php Normal file
View File

@ -0,0 +1,62 @@
<?php
/*
* 这个API支持通过post请求添加数据也支持通过post发送json数据进行修改和删除数据
* 使用pdo代替mysqli
*/
include_once '../config/pdo_connect_db18.php';
/** @var PDO $pdo */
if (isset($_POST['add'])) {
$bookname = $_POST['bookname'];
$price = $_POST['price'];
$pubdate = $_POST['pubdate'];
$sort = $_POST['sort'];
$stmt = $pdo->prepare('insert into tb_demo02 (bookname, price, f_time, type) values (?, ?, ?, ?)');
$stmt->execute([$bookname, $price, $pubdate, $sort]);
if ($stmt->rowCount() > 0) {
echo '<script>alert("成功");location.href="../19.work3.result.php";</script>';
} else {
echo '<script>alert("添加失败");history.back();</script>';
}
$stmt->closeCursor();
} else { // 原来是判断post请求中的update属性想了下数据不在那里面...
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data['update']) && $data['update']) {
$stmt = $pdo->prepare('update tb_demo02 set bookname = ?, price = ?, f_time = ?, type = ? where id = ?');
$stmt->execute([$data['bookname'], $data['price'], $data['f_time'], $data['type'], $data['id']]);
if ($stmt->rowCount() > 0) {
echo json_encode(['message' => '修改成功']);
} else {
echo json_encode(['message' => '修改失败']);
}
$stmt->closeCursor();
} elseif (isset($data['delete']) && $data['delete']) {
$stmt = $pdo->prepare('delete from tb_demo02 where id = ?');
$stmt->execute([$data['id']]);
if ($stmt->rowCount() > 0) {
echo json_encode(['message' => '删除成功']);
} else {
echo json_encode(['message' => '删除失败']);
}
$stmt->closeCursor();
} elseif (isset($data['batDelete']) && $data['batDelete']) {
$stmt = $pdo->prepare('delete from tb_demo02 where id = ?');
$ids = $data['id'];
$count = 0;
foreach ($ids as $id) {
$stmt->execute([$id]);
if ($stmt->rowCount() > 0) {
$count++;
}
}
if ($count === count($ids)) {
echo json_encode(['message' => '删除成功']);
} else {
echo json_encode(['message' => '删除失败']);
}
$stmt->closeCursor();
} else {
echo json_encode(['message' => '异常操作']);
}
}

View File

@ -0,0 +1,13 @@
<?php
$dsn = 'mysql:host=localhost;dbname=db_database18;charset=utf8';
$pdo = null;
try {
$pdo = new PDO(dsn: $dsn,
username: 'root',
password: 'chenx221');
// echo 'Connection success';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);