list.js 文件
删除图标:
td = document.createElement("td");
td.setAttribute("style","border: 1px solid black;");
cellText = document.createElement('i');
cellText.setAttribute("data-id",data[i].pid);
cellText.setAttribute("class","fa-solid fa-trash");
cellText.setAttribute("onclick", "pDelete(this)");
td.appendChild(cellText);
row.appendChild(td);
p删除(元素):
pDelete(elem) {
let id = elem.getAttribute("data-id");
fetch(`pdelete/index.php?id=${id}`, {
method: 'DELETE'
});
}
pWorks 文件:
public static function pDelete($params){
$params=json_encode($params, true);
extract(json_encode($params,true));
$db=new DBConnect();
$sql="DELETE from products WHERE pid=:$id";
$db->sqlExec($sql);
}
pdelete/index.php 文件:
<?php
require_once("../app/pWorks.php");
if ($_SERVER["REQUEST_METHOD"] == "DELETE") {
$id = isset($_GET['id']) ? $_GET['id'] : null;
if (!empty($id)) {
pWorks::pDelete($id);
echo "deleted";
}else{echo "nope";}
}
我的主键是数据库上的 pid。它不会删除任何东西。
我的主键是数据库上的 pid。它不会删除任何内容。我的主键是数据库上的 pid。它不会删除任何内容。我的主键是数据库上的 pid。它不会删除任何内容。我的主键是数据库上的 pid。它不会删除任何内容。我的主键是数据库上的 pid。它不会删除任何内容。我的主键是数据库上的 pid。它不会删除任何内容。我的主键是数据库上的 pid。它不会删除任何内容。我的主键是数据库上的 pid。它不会删除任何内容。我的主键是数据库上的 pid。它不会删除任何内容。我的主键是数据库上的 pid。它不会删除任何东西。
在
pDelete
文件中的 pWorks
函数中,您尝试从 JSON 字符串中提取参数,但您使用的是 json_encode
而不是 json_decode
。此外,无需再次对参数进行编码,因为它们已经作为 JSON 传递。因此,您应该解码 JSON 字符串:
public static function pDelete($params){
$params = json_decode($params, true);
$id = $params['id'];
$db = new DBConnect();
$sql = "DELETE FROM products WHERE pid=:id";
$db->sqlExec($sql, ['id' => $id]);
}
您应该检查响应状态并进行处理:
pDelete(elem) {
let id = elem.getAttribute("data-id");
fetch(`pdelete/index.php?id=${id}`, {
method: 'DELETE'
})
.then(response => {
if (response.ok) {
console.log("Deleted successfully");
// you can remove the row from the table if deletion was successful
} else {
console.error("Failed to delete:", response.statusText);
}
})
.catch(error => {
console.error("Error:", error);
});
}