gpt4 book ai didi

php - 根据 ID 从 MySQL 中删除一行

转载 作者:行者123 更新时间:2023-11-29 20:09:49 25 4
gpt4 key购买 nike

我正在使用 MySQL 表内容创建一个表。问题是我试图为表中的每一行(删除列)创建一个删除(x)按钮。

 <?php
$html = "<table>";
$html .= "<tr>";
$html .= "<th>id</th>";
$html .= "<th>Event type</th>";
$html .= "<th>Date</th>";
$html .= "<th>Price(€)</th>";
$html .= "<th>Description</th>";
$html .= "<th>Delete</th>";
$html .= "</tr>";

foreach($event as $e){
$html .= "<tr>";
$html .= "<td>$e->eventId</td>";
$html .= "<td>$e->eventType</td>";
$html .= "<td>$e->eventDate</td>";
$html .= "<td>$e->eventPrice</td>";
$html .= "<td>$e->eventDescr</td>";
$html .= "<td><--the delete button has to be here--></td>";
$html .= "</tr>";
}

$html .= "</table>";
echo $html;

我尝试过使用 onclick 按钮,但完全失败了。谁能给我一些建议吗?

最佳答案

我的解决方案只是众多可能的解决方案之一

1) 创建js函数来删除行并调用ajax请求从数据库中删除项目

function removeRow(item,eventId)
{
var row = $(item).parent().parent();
$.ajax(url: '(url_to)/remove.php?id='+eventId,
success: function(resp)
{
if(resp=='OK')
{
row.remove();
}
}
);

}

2)创建按钮

$html .= '<td><a href="javascript:removeRow(this,'.$e->eventId.');return false;">Delete</a></td>';

3) 创建 PHP 脚本

<?php
//connection to DB
$dbhost = "localhost";
$dbname = "dbb";
$dbuser = " ";
$dbpswd = " ";

try
{
$db = new PDO("mysql:host=".$dbhost.";dbname=".$dbname,$dbuser,$dbpswd);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//removing event
$count = $db->exec("DELETE FROM `someTable` WHERE id = {intval($id)}");
//return OK if event was correctly deleted
if($count==1)
{
echo 'OK';
}
}
catch (PDOException $e)
{
//only for development version
echo $e->getMessage();
}
die();

关于php - 根据 ID 从 MySQL 中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40203900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com