2017-04-07 199 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

相關問題