2016-02-22 79 views
-5

我有問題的功能工作,沒有當我點擊滴,所以這裏有什麼問題 我需要刪除記錄,只有當我點擊滴,所以請任何幫助 是查詢在功能下降正確?當我刷新將刪除所有記錄

 <?php 
    $sql="SELECT * FROM administrators"; 
     $record=mysql_query($sql); 

    function drop($id,$email){ 
    $q="DELETE FROM administrators WHERE id='$id' AND email='$email'"; 
    mysql_query($q); 
    } 
    ?> 
    <title>Admins Table</title> 
    <style> 
    table, th, td { 
    border: 2px solid black; 
    border-collapse: collapse; 
    } 
    #creating { 
    font:bold; 
    font-size:1.6em; 
    } 
    </style> 
    <a href='cadmin.php' id='creating'>Create New Admin</a><br/><br/> 
    <table width="920" border="1" cellpadding="1" cellspacing="1"> 
    <tr> 
    <th>Number</th> 
    <th>First Name</th>  
    <th>Last Name</th> 
    <th>Phone Number</th> 
    <th>Gender</th> 
    <th>Email</th> 
    <th>Address</th> 
    <th>Update</th> 
    <th>Delete</th> 
    </tr> 


    <?php 

    while($row=mysql_fetch_assoc($record)){ 
echo'<tr>'; 
echo'<td>'.$row['id'].'</td>'; 
echo'<td>'.$row['first_name'].'</td>'; 
echo'<td>'.$row['last_name'].'</td>'; 
echo'<td>'.$row['phone'].'</td>'; 
echo'<td>'.$row['gender'].'</td>'; 
echo'<td>'.$row['email'].'</td>'; 
echo'<td>'.$row['address'].'</td>'; 
echo'<td>'."<a href='' onclick=''>Edit</a>".'</td>'; 
echo'<td>'."<a href='' onclick='".drop($row['id'],$row['email'])."'> Drop</a>".'</td>'; 
echo'</tr>'; 
    } 

    ?> 

</table> 

    //code here 
+1

我推薦使用mysqli的 –

+1

我建議使用PDO) – Fyntasia

+3

可怕的HTML和PHP代碼。發現了很多錯誤。你不能通過'onclick'調用一個PHP函數。請了解服務器端和客戶端腳本的區別。 – Raptor

回答

0

試試這個代碼

<?php 
$sql="SELECT * FROM administrators"; 
$record=mysql_query($sql); 


function drop($id,$email){ 
$q="DELETE FROM administrators WHERE id='$id' AND email='$email'"; 
mysql_query($q); 
header("Refresh:0"); 
} 
if (isset($_GET['drop'])) { 
    $id=$_GET['id'];$email=$_GET['email']; 
drop($id,$email); 
} 
?> 
<title>Admins Table</title> 
<style> 
table, th, td { 
border: 2px solid black; 
border-collapse: collapse; 
} 
#creating { 
font:bold; 
font-size:1.6em; 
} 
</style> 
<a href='cadmin.php' id='creating'>Create New Admin</a><br/><br/> 
<table width="920" border="1" cellpadding="1" cellspacing="1"> 
<tr> 
<th>Number</th> 
<th>First Name</th>  
<th>Last Name</th> 
<th>Phone Number</th> 
<th>Gender</th> 
<th>Email</th> 
<th>Address</th> 
<th>Update</th> 


    <th>Delete</th> 
    </tr> 
<?php 

    while($row=mysql_fetch_assoc($record)){ 
    echo'<tr>'; 
    echo'<td>'.$row['id'].'</td>'; 
    echo'<td>'.$row['first_name'].'</td>'; 
    echo'<td>'.$row['last_name'].'</td>'; 
    echo'<td>'.$row['phone'].'</td>'; 
    echo'<td>'.$row['gender'].'</td>'; 
    echo'<td>'.$row['email'].'</td>'; 
    echo'<td>'.$row['address'].'</td>'; 
    echo'<td>'."<a href='' onclick=''>Edit</a>".'</td>'; 
    echo'<td>'."<a href='?  drop=ss&id=".$row['id'].'&email='.$row['email']."'>    Drop</a>".'</td>'; 
echo'</tr>'; 
} 

?>