我有一个表格,其中包含每章前面的章节和按钮。
现在我想在单击删除按钮时删除一行,并且我还要触发删除查询以从数据库中删除该行。
我尝试了2种方法从表中删除一行,但没有删除。
<!doctype html>
<html>
<head>
<title>Chapters</title>
</head>
<body>
<style>
td {
text-align: left;
}
</style>
<script>
var par = $(this).parent().parent(); //tr
par.remove();
</script>
<table id="example" style="width:50%">
<tr>
<th><font size="5">Chapters</font></th>
</tr>
<?php
$dbh = new PDO('mysql:host=174.13.54;dbname=handbook', 'airman', 'airman');
$stmt = $dbh->prepare("SELECT * FROM chapters");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) > 0)
{
foreach($results as $chapter)
{
if($chapter['type'] == 1)
{
$type = "SSgt";
}
elseif($chapter['type'] == 2)
{
$type = "TSgt";
}
elseif($chapter['type'] == 3)
{
$type = "MSgt";
}
?>
<tr>
<td><?php $chapter['id']; echo $chapter['title'];echo " " . "(" .$type.")";?></td>
<td><input type="button" value="Delete"></td>
</tr>
<?Php
}
?>
</table>
</body>
</html>
<?php
}
?>
我该怎么做?有人可以帮忙吗?
编辑:
chapterDelete.php
<!doctype html>
<html>
<head>
<title>Chapters</title>
</head>
<form method="post" action="deleteChapter.php" enctype="multipart/form-data">
<body>
<style>
td {
text-align: left;
}
</style>
<table id="example" style="width:50%">
<tr>
<th><font size="5">Chapters</font></th>
</tr>
<?php
$dbh = new PDO('mysql:host="138.75.54;dbname=handbook', 'airman', 'airman12345');
$stmt = $dbh->prepare("SELECT * FROM chapters");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) > 0)
{
foreach($results as $chapter)
{
if($chapter['type'] == 1)
{
$type = "SSgt";
}
elseif($chapter['type'] == 2)
{
$type = "TSgt";
}
elseif($chapter['type'] == 3)
{
$type = "MSgt";
}
?>
<tr>
<td><?php echo $chapter['title'];echo " " . "(" .$type.")";?></td>
<td><input type="button" class="removeRowButton" id = "<?php $chapter['id']?>" value="Delete"></td>
</tr>
<?Php
}
?>
</table>
</body>
</form>
</html>
<script
$('.removeRowButton').click(function(){
var rowID= $(this).attr('id');
$.get( "deleteChapter.php?rowID=" + rowID, function( error ) {
if(error == 0){
$('tr#' + rowID).remove();
}
else{
alert('MySQL error!');
}
});
});
</script>
<?php
}
?>
deleteChapter.php
<?php
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);
$dbh = new PDO('mysql:host=138.75.54;dbname=handbook', 'airman', 'airman12345');
$stmt = $dbh->prepare("DELETE FROM `chapters` WHERE `rowID`= '" . $_GET["rowID"]);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($result) > 0)
{
echo 'row deleted';
}
else{
echo 'row could not delete';
}
?>
点击删除按钮时没有任何结果。
编辑2:
<!doctype html>
<html>
<head>
<title>Chapters</title>
</head>
<form method="post" action="chapterDelete.php" enctype="multipart/form-data">
<body>
<style>
td {
text-align: left;
}
</style>
<table id="example" style="width:50%">
<tr>
<th><font size="5">Chapters</font></th>
</tr>
<?php
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);
$dbh = new PDO('mysql:host=1775.54;dbname=handbook', 'airman', 'airman');
$stmt = $dbh->prepare("SELECT * FROM chapters");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) > 0)
{
foreach($results as $chapter)
{
if($chapter['type'] == 1)
{
$type = "SSgt";
}
elseif($chapter['type'] == 2)
{
$type = "TSgt";
}
elseif($chapter['type'] == 3)
{
$type = "MSgt";
}
?>
<tr>
<td><?php echo $chapter['title'];echo " " . "(" .$type.")";?></td>
<td><input type="button" onClick= "this.form.submit()" value="Delete<?php $chapter['id']?>"</input></td>
</tr>
<?Php
}
?>
</table>
</body>
</form>
</html>
<?php
}
function delete($id)
{
$dbh = new PDO('mysql:host=174.138.75.54;dbname=airman_handbook', 'airman', 'airman12345');
$stmt = $dbh->prepare("DELETE FROM `chapters` WHERE `id`= " . $id);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if(count($result) > 0)
{
echo 'row deleted';
}
else{
echo 'row could not delete';
}
}
?>
我可以不使用ajax这样做吗?但它没有用。
答案 0 :(得分:1)
您需要对.php脚本进行异步(AJAX)调用,从而删除MySQL表中的行。
首先,您的表行应该具有一个具有唯一行号的ID,并且该数字也应该是应该删除该行的按钮的属性(这样当我们按下时我们知道要删除哪一行)删除按钮):
<table>
<tr id="number">
<td>Data</td>
<td><button class="removeRowButton" id="number" value="Delete this row"></td>
</tr>
</table>
这样,您可以分别与每一行进行交互。
您的PHP文件(例如“removerow.php”)应如下所示:
// Connect to MySQL database
$error= 0;
$deleteRow= mysql_query("DELETE FROM `tablename` WHERE `rowID`= '" . $_GET["rowID"] . "';") or $error= 1;
echo($error);
当你获得SUCCESS时,你将使用jQuery从可见的HTML表中删除该行:
$('.removeRowButton').click(function(){
var rowID= $(this).attr('id');
$.get( "removerow.php?rowID=" + rowID, function( error ) {
if(error == 0){
$('tr#' + rowID).remove();
}
else{
alert('MySQL error!');
}
});
});