使用PHP删除MYSQL中的表行

时间:2015-10-22 17:19:32

标签: javascript php jquery html mysql

我查看了现有的问题,我不相信他们正在回答我的问题。 我创建了一个使用PHP和MySQL填充的表。我的add函数正常工作,因此用户可以添加新行,但我希望用户删除特定行。每行都有一个删除图标,单击此按钮时我只想删除该行。

Home.php (创建表格)

<table class="table table-bordered table-striped table-responsive">
    <tr class="header">
        <td>id</td>
        <td>Rep</td>
        <td>Date</td>
        <td>Name</td>
        <td>P_O</td>
        <td>Due Date</td>
        <td>Terms</td>
        <td>Aging</td>
        <td>Open Balance</td>
        <td>remove</td>
    </tr>
    <?php 
        while($row = mysql_fetch_array($query))
        {
            $className ="";
            if ($row['Aging'] >= 45) {
                $className="danger";
            }
            else if($row['Aging'] >= 25 && $row['Aging'] <= 44) {
                $className="warning";
            }

            echo "<tr class='$className'>";
            echo "<td>".$row['id']."</td>";
            echo "<td>".$row['Rep']."</td>";
            echo "<td>".$row['Date']."</td>";
            echo "<td>".$row['Name']."</td>";
            echo "<td>".$row['P_O']."</td>";
            echo "<td>".$row['Due_Date']."</td>";
            echo "<td>".$row['Terms']."</td>";
            echo "<td>".$row['Aging']."</td>";
            echo "<td>".$row['Open_Balance']."</td>";
            echo "<td><button type='button' class='btn btn-link'><i class='iconhover fa fa-check-circle fa-2x'></i></button></td>";
        }
    ?>
</table>

这是删除按钮:

<button type='button' class='btn btn-link'><i class='iconhover fa fa-check-circle fa-2x'></i></button>

我希望删除当前单击时的行。有什么帮助吗?

这是我的新代码,但它似乎仍然没有删除该行 home.php

while($row = mysql_fetch_array($query))
{
    $className ="";
    if ($row['Aging'] >= 45) {
        $className="danger";
    }
    else if($row['Aging'] >= 25 && $row['Aging'] <= 44) {
        $className="warning";
    }

    echo "<tr class='$className'>";
    echo "<td>".$row['id']."</td>";
    echo "<td>".$row['Rep']."</td>";
    echo "<td>".$row['Date']."</td>";
    echo "<td>".$row['Name']."</td>";
    echo "<td>".$row['P_O']."</td>";
    echo "<td>".$row['Due_Date']."</td>";
    echo "<td>".$row['Terms']."</td>";
    echo "<td>".$row['Aging']."</td>";
    echo "<td>".$row['Open_Balance']."</td>";
   echo "<td><button action='deletepage.php' method='POST' value='" .$row['id']. "' class='btn btn-danger'> Delete</button></td>";
}

deletepage.php

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

if(isset($_GET['id'])){
    $userID = (int) $_GET['id'];
    if(!empty($_GET['id'])) {
        $delete = mysql_query("DELETE FROM Book1 WHERE id='$userID'");
    }
    if($delete) {
        echo "Record deleted successfully";
    }
    else {
        echo "Sorry, record could not be deleted";
    }
  }

4 个答案:

答案 0 :(得分:4)

您可以将其指向删除页面,如下所示:

$ + -

或者使用javascript AJAX调用:

<a href="deletepage.php?<?php echo $_POST['userid']; ?>" class="btn btn-danger"> Delete</a>

并将 <a href="#" class="btn btn-danger" onclick="confirmDeletion(<?php echo $_POST['userid']; ?>);"> Delete</a> 用于删除页面

<强>更新 删除页面 deletepage.php 可能包含以下内容:

$.post

<强> users.php

<?php
require('dbconn.php');
if(isset($_POST['id'])){
    $userID = (int) $_POST['id'];
    if(!empty($_POST['id'])){
        $delete = mysql_query("DELETE FROM users WHERE id='$userID'");
    }
    if($delete){
        echo "Record deleted successfully";
    }else{
        echo "Sorry, record could not be deleted";
    }
}
?>

<强> P.S: 建议以这种方式使用<?php require('dbconn.php'); $get = mysql_query("SELECT * FROM users"); while($row = mysql_fetch_array($get)) { echo '<p>'; echo $row['id'] . ' - ' . $row['user']; ?> <a href="#" style="color:red;" onclick="confirmDeletion(<?php echo $row['id']; ?>);"> Delete</a> </p> <?php } ?> <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> <script> function confirmDeletion(id){ $.post('deletepage.php', { id:id }, function(data){ alert(data); location.reload(); }); } </script> 退出,而是使用PDO来避免SQL注入

答案 1 :(得分:1)

我会通过在按钮html中添加一个value属性来更改按钮以包含行id的值:

value='" .$row['id']. "'

然后,您可以使用jquery / javascript捕获click事件,并使用ajax调用从数据库中删除记录。它看起来像是:

$('.btn-lnk').on('click',function() {
      var id = $(this).val();
      $.ajax({
          type: "POST",
          url: "yourPageThatDeletesRow.php",
          data: { id: id },
          success: function(response) {
              if(response === 'success') {
                 //delete row showing on the page
                 $(this).closest('tr').remove();
              } else {
                  //handle error
              }
          } //consider handling ajax error case
      });
});

ajax调用将执行yourThatDeletesRow.php上的代码。您可以使用$ _POST [&#39; id&#39;]获取行ID,并使用该ID删除数据。如果删除成功,请回显字符串&#39; success&#39;。如果它不成功,请考虑从数据库返回错误,并在返回ajax时处理该情况

这是一个简化的JS Fiddle,显示了id和行删除的传递方式。

答案 2 :(得分:0)

如果你按照我的方式去做,你将需要一个删除按钮。

if (isset($_POST['delete'])){
$userid = $_POST['userid'];
$sql = "DELETE FROM users WHERE userid = '$userid';";
if ($conn->query($sql) === true){
//echo '<a href="dbtablelist.php">Click here</a> to view modified    patients';

}
}

答案 3 :(得分:-1)

使用此代码:

<?php $temp_pre_ID = $row['id'];?>
<INPUT TYPE="button" onClick="window.location='home.php?Action=del&CusID=<?php echo $temp_pre_ID;?>'" value="Delete">