2017-04-07 194 views
1

我试图在每行上添加一个删除按钮,这样我可以在按下按钮时删除一条记录。我是PHP和MySQL以及Stack Overflow的新手。在MySQL表格的每一行上添加一个PHP删除按钮

下面是我从我的MySQL数据库中提取信息的工作表。

 <table class="table" > 
     <tr> 
     <th> Staff ID </th> 
     <th> Staff Name </th> 
     <th> Class </th> 
     <th> Action </th> 

     </tr> 

     <?php 

     while($book = mysqli_fetch_assoc($records)){ 

     echo "<tr>"; 
     echo "<td>".$book['Staff_ID']."</td>"; 
     echo "<td>".$book['Staff_Name']."</td>"; 
     echo "<td>".$book['Class']."</td>"; 
     echo "</tr>"; 
     }// end while loop 
+0

的http://计算器。 com/questions/43281598/how-to-delete-a-specific-row-in-a-table-using-javascript#comment73631786_43281598 – mkaatman

回答

5

只需用PHP如下(可以使用JS)

while($book = mysqli_fetch_assoc($records)){ 

echo "<tr>"; 
echo "<td>".$book['Staff_ID']."</td>"; 
echo "<td>".$book['Staff_Name']."</td>"; 
echo "<td>".$book['Class']."</td>"; 
echo "<td><a href='delete.php?id=".$book['Staff_ID']."'></a></td>"; //if you want to delete based on staff_id 
echo "</tr>"; 
}// end while loop 

在你delete.php文件,

$id = $_GET['id']; 
//Connect DB 
//Create query based on the ID passed from you table 
//query : delete where Staff_id = $id 
// on success delete : redirect the page to original page using header() method 
$dbname = "your_dbname"; 
$conn = mysqli_connect("localhost", "usernname", "password", $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

// sql to delete a record 
$sql = "DELETE FROM Bookings WHERE Staff_ID = $id"; 

if (mysqli_query($conn, $sql)) { 
    mysqli_close($conn); 
    header('Location: book.php'); //If book.php is your main page where you list your all records 
    exit; 
} else { 
    echo "Error deleting record"; 
} 
+1

非常感谢你,这已经工作:)希望亩大学教师这样回应!欢呼声:) –

+0

<?php //连接并输入到数据库和服务器 $ db = mysqli_connect(“server”,“usernname”,“password”); if(!$ db) { echo“未连接到服务器”; } if(!mysqli_select_db($ db,“username”)) { echo“database not selected”; } 要求'book.php'; //这是我们创建按钮的文件 $ id = $ _GET ['id']; $ sql_delete =“DELETE FROM Bookings WHERE Staff_ID = $ id”; header(“location:book.php”); ?> @webDev我在这里做错了什么?对此感到抱歉 –

+0

看到更新的代码,你不需要book.php @HamzahAmir – webDev

2

创建收到$ _GET [“身份证”]一个delete.php文件,然后运行SQL当他们去到该页面删除记录。通过两种方式完成:如下所示的锚定标记,

或者制作一个按钮而不是锚点,运行ajax(通过jquery)发送该id并运行上面提到的delete.php脚本。

table class="table" > 
     <tr> 
     <th> Staff ID </th> 
     <th> Staff Name </th> 
     <th> Class </th> 
     <th> Action </th> 

     </tr> 

     <?php 

     while($book = mysqli_fetch_assoc($records)){ 

     echo "<tr>"; 
     echo "<td>".$book['Staff_ID']."</td>"; 
     echo "<td>".$book['Staff_Name']."</td>"; 
     echo "<td>".$book['Class']."</td>"; 
     echo "<td><a href='delete.php?id=".$book['Staff_ID']."'>Delete</a></td>"; 
     echo "</tr>"; 
     }// end while loop 
0

我会建议你使用Ajax来拨打电话,有事就像@webDev一样,但不是调用PHP,而是使用AjaxCall调用Javascript,然后使用JS隐藏/删除相关行,这样用户就不必重新加载整个页面。

你可以补充你选择这个代码,而不是HREF为正确答案:

echo '<td><button onclick="deleteRow('.$book['Staff_ID'].')">Delete</button></td>'; 

而在<head>部分添加以下功能:

<script> 
    function deleteRow(StaffID){ 
     var xhttp = new XMLHttpRequest(); 
     xhttp.onreadystatechange = function() {      

     if (xhttp.readyState == 4 && xhttp.status == 200) { 
        alert("Deleted!"); 
      } 
     }; 
     document.getElementById("table").deleteRow(x); 
     xhttp.open("GET", "delete.php", true); 
     xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     xhttp.send("id="+StaffID); 
     }  
</script> 
+2

是的,我知道,但发布该问题的用户是初学者,对于初学者来说,尽可能多地有简单的答案。首先学习使用PHP更好地理解它 – webDev

相关问题