【问题标题】:How to delete a row of table on click of a button and respective data from database?如何在单击按钮时删除一行表和数据库中的相应数据?
【发布时间】:2017-03-25 15:03:58
【问题描述】:

我有一个包含章节的表格,每个章节前面都有一个按钮。

现在我想在单击删除按钮时删除一行,并且我想触发删除查询以从数据库中删除该行。

我尝试了 2 3 种方法从表中删除一行,但它没有被删除。

  <!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

}

?>

删除章节.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 的情况下这样做吗?但它不起作用。

【问题讨论】:

  • 从表名中删除其中 id = $tbl_id
  • 我知道我需要知道如何在 html 或 java 脚本中实现它的查询?@Chris
  • 您似乎还没有创建表单,只是一个输入,因此您需要对其进行排序以提交或使用 javascript 来监听点击收集所需的信息并向服务器提交 ajax 调用运行 php 代码。
  • Google jquery ajax,试试看,如果你有问题再回来 - 请不要让别人为你写完整的解决方案
  • 你不用html或javascript实现,你用php实现,然后用html或javascript触发。

标签: php jquery mysql button


【解决方案1】:

您需要对从 MySQL 表中删除该行的 .php 脚本进行异步 (AJAX) 调用。

首先,您的表格行应该有一个带有唯一行号的 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!');
    }
});

});

【讨论】:

  • 我试图实现你的答案,但点击删除按钮没有任何反应,你能检查一下编辑过的问题吗?
  • 我在您的代码中的任何地方都没有看到.click() 函数,所以难怪什么都没有发生?您需要在某处添加一个功能,以便在按下按钮时执行某些操作!
  • 如何添加这个,点击功能?我可以在不使用 ajax 的情况下做到这一点吗?你能检查一下编辑过的问题吗?
  • 您没有将表格行的 id 传递给您的 php 脚本。提交按钮中的 Value="Delete" 完全没有任何作用。例如,您需要为每个按钮制作一个单独的表单并传递表格行 ID,或者使用 Ajax 执行服务器端部分并使用我建议的 jquery 脚本来进行 GET 调用。
  • 在我上面的回答中。
猜你喜欢
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 2020-04-16
  • 1970-01-01
  • 2016-01-04
  • 1970-01-01
  • 2012-05-23
相关资源
最近更新 更多