2015-10-09 67 views
3

我是PHP MySQL初學者,我想知道如何爲這個每個特定記錄創建一個DELETE函數。刪除腳本PHP MySQL

enter image description here

這裏是我的代碼時,我選擇的記錄。我不知道我將如何插入DELETE函數以及代碼結構如何。

$res=mysql_query("SELECT * FROM accounts WHERE user_id=".$_SESSION['user']); 
$userRow=mysql_fetch_array($res); 


$sql = "SELECT * FROM accounts ORDER BY agentFname ASC"; 

if($result = mysql_query($sql)){ 
    if (mysql_num_rows($result) > 0) { 



      <table id="example" class="table table-striped responsive-utilities jambo_table"> 

       <thead> 

         <tr class="headings"> 

       <?php   

        echo "<th>ID</th>"; // Primary Key 
        echo "<th>Agent Code</th>"; // Unique Key 
        echo "<th>First Name</th>"; 
        echo "<th>Middle Name</th>";  
        echo "<th>Last Name</th>";  
        echo "<th>Contact Number</th>";  
        echo "<th>Address</th>";  
        echo "<th>Gender</th>"; 
        echo "<th>User Type</th>"; 
        echo "<th>Action</th>";   
        echo "</tr>"; 
        echo "</thead>"; 

      } while($row = mysql_fetch_array($result)) { 

       echo"<tbody>"; 
       echo " <tr class=\"even pointer\">"; 

       echo "<td>" .$row['user_id']. "</td>"; // Primary Key 
       echo "<td>" .$row['agentCode']. "</td>"; // Unique Key 
       echo "<td>" .$row['agentFname']. "</td>"; 
       echo "<td>" .$row['agentMname']. "</td>"; 
       echo "<td>" .$row['agentLname']. "</td>"; 
       echo "<td>" .$row['agentContact']. "</td>"; 
       echo "<td>" .$row['agentAddress']. "</td>"; 
       echo "<td>" .$row['agentGender']. "</td>"; 
       echo "<td>" .$row['user_type']. "</td>"; 
       echo "<td> <a href=\"#.php\"> Delete</a></td>"; // DELETE Function 
       echo "</tr>"; 
       } 

      } 
+0

我希望那些都不是真正的電話號碼... – rikitikitik

+0

是的,先生:)這不是 – Edmhar

回答

1

只是通過用戶ID與HREF文件名。

echo "<td> <a href="yourfile.php?id=<?php echo $row['user_id'];?>"> Delete</a></td>"; 

然後創建您的文件。 例如您filename.php

<?php 

$id=$_GET['id']; 

$sql=mysql_query("DELETE FROM tablename where user_id='$id'"); 

if($sql > 0) 
{ 
    echo "record successfully deleted"; 
} 
else 
{ 
echo "errore in deleting"; 
} 

?> 
+0

你太棒了!謝謝! – Edmhar

+0

@mak mysql_query已被棄用,你不應該鼓勵人們使用,使用mysqli或者pdo代替mysql –

2

看起來像你試着做一些像

echo "<td><a href="/yourfile.php?delete=<?php echo $row['user_id'];?>"> Delete</a></td> 

然後yourfile.php你會處理刪除和重定向回到原來的頁面,但...

這將刪除即使您錯誤地點擊那裏記錄。

你可以做小的形式有一個複選框每次還是有一些JS要求先確認刪除...

+0

感謝。但它太接近了。 – Edmhar

+0

我在看的是delete.php代碼。順便會嘗試使用複選框的建議。謝謝! – Edmhar