首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何一键删除一行表格和数据库中的相应数据?

问如何一键删除一行表格和数据库中的相应数据?
EN

Stack Overflow用户
提问于 2017-03-25 23:03:59
回答 1查看 2.4K关注 0票数 0

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

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

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

代码语言:javascript
复制
  <!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

}

?>

我该怎么做呢?有谁能帮帮忙吗?

编辑:

代码语言:javascript
复制
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

代码语言:javascript
复制
    <?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';    
}

?>

单击delete按钮时没有任何反应。

编辑2:

代码语言:javascript
复制
    <!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的情况下这样做吗?但它不起作用。

EN

回答 1

Stack Overflow用户

发布于 2017-03-25 23:12:55

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

首先,您的表行应该有一个设置了唯一行号的ID,并且这个数字也应该是应该删除该行的按钮的属性(这样我们就可以知道在按下remove按钮时要删除哪一行):

代码语言:javascript
复制
<table>
    <tr id="number">
        <td>Data</td>
        <td><button class="removeRowButton" id="number" value="Delete this row"></td>
    </tr>
</table>

这样,您就可以分别与每一行进行交互。

你的PHP文件(例如"removerow.php")应该是这样的:

代码语言:javascript
复制
// Connect to MySQL database

$error= 0;

$deleteRow= mysql_query("DELETE FROM `tablename` WHERE `rowID`= '" . $_GET["rowID"] . "';") or $error= 1;

echo($error); 

当您获得成功后,您将使用jQuery从可见的HTML表中删除行:

代码语言:javascript
复制
$('.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!');
    }
});

});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43017950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档